0

I have a dataframe (responses) of survey responses.

> responses <- data.frame(var1 = c(1,2,2,3,1,2,1), var2 = c("a","a","a","b","a","c","c"))
> responses
  var1 var2
1    1    a
2    2    a
3    2    a
4    3    b
5    1    a
6    2    c
7    1    c

I want to create a column of ids for responses. I also have a dataframe (pairs) with two columns that correspond to row indices from responses that should have the same id.

pairs <- data.frame(x=c(1,1,2,3), y=c(4,5,1,6))
> pairs                    
  x y
1 1 4
2 1 5
3 2 1
4 3 6    

In this example, rows 1,2,4, and 5 should have the same id, 3 and 6 should have the same id, and 7 should have a unique id. Desired output:

> responses
  var1 var2 id
1    1    a  1
2    2    a  1
3    2    a  2
4    3    b  1
5    1    a  1
6    2    c  2
7    1    c  3

What is the simplest way to accomplish this in R?

James Martherus
  • 1,033
  • 1
  • 9
  • 20
  • 2
    [This answer](https://stackoverflow.com/a/45079785/3460670) or [this one](https://stackoverflow.com/a/38663293/3460670) may be helpful using `igraph`... – Ben Sep 13 '22 at 00:55
  • Those solutions worked great, thank you. Voting to close as a duplicate. – James Martherus Sep 13 '22 at 23:29

0 Answers0