简体   繁体   English

如何通过R中另一个因子的因子级别对数据帧进行子集化?

[英]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.

相关问题 如何最好地在 R 中的数据帧中将一个因子的不同级别彼此划分? - How best to divide different levels of a factor by one another in dataframe in R? R:基于允许因子水平列表的子集数据框 - R: Subset dataframe based on list of allowed factor levels 子集 dataframe 基于 R 中列内因子水平的分层偏好 - subset dataframe based on hierarchical preference of factor levels within column in R R:与另一个因子的两个水平同时出现的子集因子水平 - R: Subset factor levels that co-occur with two levels from another factor 当 NA 存在时如何在因子水平上对数据框进行子集化 - How to subset dataframe on factor levels when NA are present 如何在R中绘制混合模型的因子水平的子集 - How to plot a subset of the factor levels of a mixed model in R 对于一个因子的所有级别,请使用dplyr从同一数据帧返回另一个因子的所有级别。 [R - For all levels of a factor, return all levels of another factor from same dataframe - using dplyr ? r 计算另一个因子中的因子水平 - R - Count factor levels inside another factor - R 使用 R 中因子 self 的子集重命名因子水平 - Renaming factor levels using a subset of the factor self in R 如何按R中的因子水平的预定顺序对数据帧进行排序? - How can I sort a dataframe by a predetermined order of factor levels in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM