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?