Reputation: 5
I have this dataframe df
:
A B
value1 a
value1 b
value1 c
value2 a
value2 b
value 2 c
value 2 d
And I'd like to have something like this
A B group
value1 a 1
value1 b 1
value1 c 1
value2 a 2
value2 b 2
value2 c 2
value2 d 2
That is, I'd like to create a variable that groups the same A
values. How would you say it is the best way to do this?
Example
id code
2012-10-20 R
2012-10-20 L
2012-10-20 K
2015-04-28 K
2015-04-28 L
2015-04-28 L
Desidered:
id code group
2012-10-20 R 1
2012-10-20 L 1
2012-10-20 K 1
2015-04-28 K 2
2015-04-28 L 2
2015-04-28 L 2
Thank you!
Upvotes: 0
Views: 70
Reputation: 488
If you just want to use the values in A as a group, then you already have that. If you want to create an ordered grouping from A and A is an ordered variable (date, numeric, factor, etc) then do something like:
library(dplyr)
df %>% mutate(group = dense_rank(A))
Upvotes: 0
Reputation: 7816
Probably the simplest way to do this is with as.factor
df$group <- as.numeric(as.factor(df$id))
Upvotes: 2