getting shared OTU from multiple dataframes

Viewed 35

so I would like to get shared taxa from different dataframes in R, where the columns reflect the samples and the OTUS are in each row. in this case I used 4 dataframes. here is the code to simulate the dataframes:

df1 <- data.frame(replicate(10,sample(0:1,10,rep=TRUE)))
df2 <- data.frame(replicate(10,sample(0:1,10,rep=TRUE)))
df3 <- data.frame(replicate(10,sample(0:1,10,rep=TRUE)))
df4 <- data.frame(replicate(10,sample(0:1,10,rep=TRUE)))

Then, I would like to see which OTUS in each sample are shared accross the dataframe. for that, I try to convert these df into a list and used lapply

list_df <- list(df1, df2, df3, df4)
test <- data.frame(lapply(df,
               function(x) ifelse(x>0, 1, 0)))

Here, I assume that if OTU is presence, its count will be more than 0. then, if it is non zero in all dataframes, this OTU is shared. the expected result I can think of is as follow:

      samp1    samp2   samp3
otu1    1        1       0
otu2    0        1       0
otu3    1        0       0

where 1 means that the OTU is shared in the corresponding sample from all dataframes, and 0 means that the OTU is either absence completely or not shared by all samples from all dataframes.

However, this code does not work and I am struggling to look for a right way to do it. So, if you guys have opinion on this, please kindly let me know as I am pretty new in this matter. Thanks in advance!

0 Answers
Related