Rock
Rock

Reputation: 2967

How to add group ID by same rows?

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!

Upvotes: 2

Views: 633

Answers (2)

seancarmody
seancarmody

Reputation: 6290

How about this:

df$group <- as.numeric(factor(with(df, paste(x,y))))

...very similar to the other solution.

Upvotes: 1

David Robinson
David Robinson

Reputation: 78590

I think this is what you're looking for:

df$group = df$x:df$y
#  x y group
#1 A C   A:C
#2 B C   B:C
#3 B C   B:C
#4 C B   C:B
#5 C B   C:B
#6 C A   C:A

group in this case is a factor. If you want it to have numeric IDs:

df$group = as.numeric(df$x:df$y)
#  x y group
#1 A C     3
#2 B C     6
#3 B C     6
#4 C B     8
#5 C B     8
#6 C A     7

ETA: If you want the group IDs to be consecutive integers:

df$group = match(df$x:df$y, unique(df$x:df$y))
#  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

Upvotes: 4

Related Questions