简体   繁体   中英

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.

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. Any idea on how to do this? I looked at the merge() function but could not get it it to work.

替代解决方案可能是:

B[B$factor%in%A$factor,]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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