[英]How to subset a dataframe by factor levels of another in R?
I have two daframes (A,B) that both contain exactly the same columns. 我有两个daframe(A,B),它们都包含完全相同的列。
A<-data.frame(factor=c("A","B","C"),value=c(1,2,3))
B<-data.frame(factor=c("A","B","C","D","E"),value=c(7,8,9,4,5))
At the end I would like to have a subset of B
which only contains the levels of A$factor
(A,B,C) and the corresponding values. 最后,我希望有一个
B
的子集,它仅包含A$factor
(A,B,C)的级别和相应的值。 Any idea on how to do this? 关于如何执行此操作的任何想法? I looked at the
merge()
function but could not get it it to work. 我查看了
merge()
函数,但无法使其正常工作。
替代解决方案可能是:
B[B$factor%in%A$factor,]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.