R从数据帧中提取不匹配的行

时间:2014-09-03 16:51:47

标签: r matrix dataframe extraction

我有两个具有相似rownames的数据框(例如a和b)。如何提取data.frame a中不存在的data.frame b中的行?

感谢, JD

3 个答案:

答案 0 :(得分:1)

a[!rownames(a)%in%rownames(b),]

答案 1 :(得分:1)

这个怎么样?

a <- data.frame(V1 = rnorm(10), V2 = rnorm(10))
row.names(a) <- paste("row", row.names(a))

> a
                V1          V2
row 1  -0.26695225 -1.72322854
row 2   0.17966753 -2.59861185
row 3   0.09679525 -0.18304155
row 4  -0.21904183 -0.67646542
row 5  -0.64441966 -0.58033626
row 6   0.28901284  0.01743280
row 7  -1.38555961  0.09184929
row 8  -0.60452736  0.08304690
row 9  -0.03898833  0.01075546
row 10 -0.21569958 -0.25948764

b <-data.frame(V1 = rnorm(5), V2 = rnorm(5))
row.names(b) <- paste("row", row.names(b))

> b
             V1         V2
row 1  1.412045 -0.6990107
row 2 -1.433099  0.8693957
row 3  1.061640  1.2938895
row 4 -1.715340 -0.6044137
row 5  1.131158 -0.3148006

a[setdiff(row.names(a), row.names(b)),]

> a[setdiff(row.names(a), row.names(b)),]
                V1          V2
row 6   0.28901284  0.01743280
row 7  -1.38555961  0.09184929
row 8  -0.60452736  0.08304690
row 9  -0.03898833  0.01075546
row 10 -0.21569958 -0.25948764

答案 2 :(得分:0)

使用?setdiff,如下所示:

a[ setdiff( rownames(a), rownames(b) ), ]