【发布时间】:2020-06-18 19:19:19
【问题描述】:
我的两个数据框如下:
df1 <- structure(list(species = structure(1:4, .Label = c("a", "b",
"c", "d"), class = "factor"), sample1 = c(1L, 1L, 1L, 1L), sample2 = c(0L,
0L, 1L, 1L)), class = "data.frame", row.names = c(NA, -4L))
df2 <- structure(list(species = structure(c(1L, 5L, 6L, 7L, 2L, 3L,
4L), .Label = c("a", "b", "c", "d", "x", "y", "z"), class = "factor"),
sample1 = c(1L, 1L, 0L, 1L, 0L, 1L, 1L), sample2 = c(1L,
1L, 1L, 0L, 1L, 1L, 1L)), class = "data.frame", row.names = c(NA,
-7L))
1/0 表示存在和不存在。
现在我想将 df1 的每一列与 df2 中的对应列进行匹配,并将比较结果保存在两个参数中(对于 df1 中的每一列)。
TP - 每列中与对应的 df2 非零值匹配的非零 df1 值的数量和
FP - 每列中与对应的 df2 非零值不匹配的非零 df1 值的数量。
输出数据帧(df3)应该是:
df3<-structure(list(species = structure(c(1L, 2L, 3L, 4L, 6L, 5L), .Label = c("a",
"b", "c", "d", "FP", "TP"), class = "factor"), sample1 = c(1L,
1L, 1L, 1L, 3L, 1L), sample2 = c(0L, 0L, 1L, 1L, 2L, 0L)), class = "data.frame", row.names = c(NA,
-6L))
我尝试使用 setdiff 来获取 df1 中的差异:
overlap <- for ( i in 1:colnames(df1)){
data.frame(setdiff(df1[,i], df2[,i]) >0)
}
但显然这不是正确的方法。
感谢您的帮助!
【问题讨论】:
-
嗨,你是对的,我现在换 df3
标签: r