Suppose I have:
df <- data.frame(x = rep(as.factor(LETTERS[1:3]), c(1, 2, 3)))
df:
x
1 A
2 B
3 B
4 C
5 C
6 C
How can I add another column (group ID) based on the same letter on df$x:
x group
1 A 1
2 B 2
3 B 2
4 C 3
5 C 3
6 C 3
Thanks!
————-EDIT————–
Sorry, guess I should rephrase my question. Here we have:
df <- data.frame(x = rep(as.factor(LETTERS[1:3]), c(1, 2, 3)),
y = rep(as.factor(LETTERS[3:1]), c(3, 2, 1)))
df
x y
1 A C
2 B C
3 B C
4 C B
5 C B
6 C A
And I need a column of group ID to separate x & y pairs:
df
x y group
1 A C 1
2 B C 2
3 B C 2
4 C B 3
5 C B 3
6 C A 4
Thanks for help!
I think this is what you’re looking for:
groupin this case is a factor. If you want it to have numeric IDs:ETA: If you want the group IDs to be consecutive integers: