简体   繁体   English

如何根据来自另一个 DataFrame 的行从一个 DataFrame 中删除行?

[英]How to remove rows from one DataFrame based on rows from another DataFrame?

I have two dataframes.我有两个数据框。 I would like to match the rownames of the two dataframes, and if the rownames won't match, I would like to remove theses rows from both dataframe to match the dimension of rows.我想匹配两个数据框的行名,如果行名不匹配,我想从 dataframe 中删除这些行以匹配行的维度。

Dataframe A:

5002T  0 1
B01167 1 0
C1329  1 0

dim(3, 3)

Dataframe B:

5002T  4
B41167 3 
C1329  8 
2C134  1 

dim(4, 2)

The Output will be look like the following: Output 将如下所示:

Dataframe A_New:

5002T  0 1
C1329  1 0

dim(2, 3)

Dataframe B_New:

5002T  4 
C1329  8 

dim(2, 2)

Here is the code which I have tried to match the rownames:这是我尝试匹配行名的代码:

match <- rownames(A) %in% rownames(B)
sum(!match)

How can I remove the unmatched rows from both dataframes and get the same dimension.如何从两个数据框中删除不匹配的行并获得相同的维度。

You were close.你很亲密。

dfa[rownames(dfa) %in% rownames(dfb), ]
#        V2 V3
# X5002T  0  1
# C1329   1  0

dfb[rownames(dfb) %in% rownames(dfa), , drop=FALSE]
#        V2
# X5002T  4
# C1329   8

However, it might be easier using intersect .但是,使用intersect可能更容易。

mtch <- intersect(rownames(dfa), rownames(dfb))

dfa[mtch, ]
#        V2 V3
# X5002T  0  1
# C1329   1  0

dfb[mtch, , drop=FALSE]
#        V2
# X5002T  4
# C1329   8

Note: drop=FALSE is needed because in this special case R would coerce a one-columned data frame in a vector.注意: drop=FALSE是必需的,因为在这种特殊情况下,R 将强制向量中的单列数据帧。


Data:数据:

dfa <- structure(list(V2 = c(0L, 1L, 1L), V3 = c(1L, 0L, 0L)), row.names = c("X5002T", 
"B01167", "C1329"), class = "data.frame")

dfb <- structure(list(V2 = c(4L, 3L, 8L, 1L)), row.names = c("X5002T", 
"B41167", "C1329", "X2C134"), class = "data.frame")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM