Null-Hypothesis Null-Hypothesis -4 years ago 87
R Question

set intersection from list containing data frames

I have a list where each element in the list is a data frame.

> df.list[[1]]
Change Diff VarName
1 10.433354 5.311973e-02 a
2 4.587958 1.517604e-02 b
3 4.566829 1.082679e-02 c
4 4.464458 1.345807e-02 d
5 4.146909 7.758011e-03 e
6 4.141556 1.416043e-02 f

> df.list[[2]]
Change Diff VarName
1 12.443354 5.311973e-02 j
2 3.587958 1.517604e-02 k
3 4.566829 1.082679e-02 a
4 4.464458 1.345807e-02 b
5 3.146909 7.758011e-03 d
6 2.141556 1.416043e-02 e


Length of my list is 10 where there are 10 data frames. each data frame has 30 items. I want to
intersect
each data frame's top 25 with another and finally get most common items in all data frames.

Here is what I have done so far:

df1 <- df.list[[1]]$VarName
df2 <- df.list[[2]]$VarName
df3 <- df.list[[3]]$VarName
df4 <- df.list[[4]]$VarName

intersect(intersect(intersect(df1,df2), df3), df4)


Is there away to do this using
dplyr
or any other tool.

Answer Source

Try Reduce in base R:

ls <- vector("list", 10)
for (i in 1:10) ls[[i]] <- head(df.list[[i]]$VarName, 25)
Reduce(intersect, ls)

Or a one-liner code:

Reduce(intersect, lapply(df.list, function(x) head(x$VarName, 25)))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download