【问题标题】:Comparing two dataframes and outputting unique values比较两个数据帧并输出唯一值
【发布时间】:2014-09-24 08:20:00
【问题描述】:

我有两个数据框,

A(514 rows)

1 2
2 3      
4 5
1 3
5 6
...

B(696 rows)

1 2
3 4
4 5
1 3
5 7
5 6
.....

我想获取 B 中存在但 A 中不存在的那些行及其对应的列。 例如,结果将是,

3 4
5 7

如何在 R 中做到这一点?

我尝试使用这个帖子的答案:Compare two data.frames to find the rows in data.frame 1 that are not present in data.frame 2

但是在这里,第一个答案差异没有给出正确的输出。

【问题讨论】:

  • 有点不清楚。您想要 B 中的匹配列,而不是 A 中的行吗?

标签: r


【解决方案1】:

试试:

colIndx <- colnames(B)[colnames(B) %in% colnames(A)]
rowIndx <- !as.character(interaction(B)) %in%  as.character(interaction(A[colIndx]))
 B[rowIndx,]
#   V1 V2
#2  3  4
#5  5  7

数据

 A <- structure(list(V1 = c(1L, 2L, 4L, 1L, 5L), V2 = c(2L, 3L, 5L, 
 3L, 6L)), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA, 
-5L))

 B <- structure(list(V1 = c(1L, 3L, 4L, 1L, 5L, 5L), V2 = c(2L, 4L, 
 5L, 3L, 7L, 6L)), .Names = c("V1", "V2"), class = "data.frame", row.names = c(NA, 
-6L))

【讨论】:

    猜你喜欢
    • 2019-10-03
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    • 2017-08-16
    • 2019-07-21
    • 1970-01-01
    相关资源
    最近更新 更多