按多个因子级别对数据框进行子集 [英] Subset a dataframe by multiple factor levels

查看:33
本文介绍了按多个因子级别对数据框进行子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何避免使用循环根据多个因素级别对数据帧进行子集化?

How can I avoid using a loop to subset a dataframe based on multiple factor levels?

在下面的例子中,我想要的输出是一个数据帧.数据框应包含原始数据框的行,其中Code"中的值等于selected"中的值之一.

In the following example my desired output is a dataframe. The dataframe should contain the rows of the original dataframe where the value in "Code" equals one of the values in "selected".

工作示例:

#sample data
Code<-c("A","B","C","D","C","D","A","A")
Value<-c(1, 2, 3, 4, 1, 2, 3, 4)
data<-data.frame(cbind(Code, Value))

selected<-c("A","B") #want rows that contain A and B

#Begin subsetting
result<-data[which(data$Code==selected[1]),]
s1<-2
while(s1<length(selected)+1)
{
  result<-rbind(result,data[which(data$Code==selected[s1]),])
  s1<-s1+1
}

这是一个更大数据集的玩具示例,因此selected"可能包含大量元素和大量行数据.因此我想避免循环.

This is a toy example of a much larger dataset, so "selected" may contain a great number of elements and the data a great number of rows. Therefore I would like to avoid the loop.

推荐答案

可以使用%in%

  data[data$Code %in% selected,]
  Code Value
1    A     1
2    B     2
7    A     3
8    A     4

这篇关于按多个因子级别对数据框进行子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆