adm - 1 month ago 6
R Question

Finding intersections of sets in R

I have the following data-frame, and want to return the index from this data-frame that corresponds to the vector [1 1 1 1 0 0 1]. Func. should return 5 in this case.

1: 0 0 1 1 0 0 1

2: 1 0 1 1 0 0 1

3: 2 0 1 1 0 0 1

4: 0 1 1 1 0 0 1

5: 1 1 1 1 0 0 1

6: 2 1 1 1 0 0 1

7: 0 0 2 1 0 0 1

What about if we have another data.frame consisting of

1: 0 0 2 1 0 0 1 == 7

2: 0 1 1 1 0 0 1 == 4

2: 2 0 1 1 0 0 1 == 3

4: 2 0 1 1 0 0 1 == 3

5: 2 1 1 1 0 0 1 == 6

I would like the function to return 7,4,3,3,6. I would like to accomplish this without using a loop. Thank you!

For the second (and effectively the first question as well since you can add whatever rows you want to the data frame) question:

``````mydf\$rownum <- 1:nrow(mydf)
merge(mydf.sub, mydf, sort=F)[[8]]
``````

Produces:

``````[1] 7 4 3 3 6
``````

Or without the `[[8]]`:

``````  V1 V2 V3 V4 V5 V6 V7 rownum
1  0  0  2  1  0  0  1      7
2  0  1  1  1  0  0  1      4
3  2  0  1  1  0  0  1      3
4  2  0  1  1  0  0  1      3
5  2  1  1  1  0  0  1      6
``````

If you want the answer to the first question baked in:

``````merge(rbind(mydf.sub, c(1, 1, 1, 1, 0, 0, 1)), mydf, sort=F)
``````

Produces:

``````  V1 V2 V3 V4 V5 V6 V7 rownum
1  0  0  2  1  0  0  1      7
2  0  1  1  1  0  0  1      4
3  2  0  1  1  0  0  1      3
4  2  0  1  1  0  0  1      3
5  2  1  1  1  0  0  1      6
6  1  1  1  1  0  0  1      5
``````

Reproducible data (thanks Ananda for the First part):

``````structure(list(V1 = c(0L, 0L, 2L, 2L, 2L), V2 = c(0L, 1L, 0L,
0L, 1L), V3 = c(2L, 1L, 1L, 1L, 1L), V4 = c(1L, 1L, 1L, 1L, 1L
), V5 = c(0L, 0L, 0L, 0L, 0L), V6 = c(0L, 0L, 0L, 0L, 0L), V7 = c(1L,
1L, 1L, 1L, 1L)), .Names = c("V1", "V2", "V3", "V4", "V5", "V6",
"V7"), class = "data.frame", row.names = c(NA, -5L))
``````
Source (Stackoverflow)