[英]Subsetting two factors — ‘&’ not meaningful for factors
I am trying to filter two variables in my data.frame
by specified conditions. 我试图按指定条件过滤
data.frame
的两个变量。
I want to subset two variable factors: a) Ratings b) Location 我想将两个可变因素归为一个子集:a)评分b)位置
I filtered the ratings variable in order to select those ratings which are relevant to me, ie, ratings == 0. 我筛选了等级变量,以便选择与我相关的那些等级,即等级== 0。
Now, I would now like select the 'Location' variable that only have a Ratings == 0, but am unable to do so. 现在,我现在想选择仅具有Ratings == 0,但无法执行此操作的“ Location”变量。 I know it is subsetting but cant get code right....But not filtering the ''location'' variable my location variable will number more than my Rating variable.
我知道它是子集,但无法正确获取代码。...但是不过滤“ location”变量,我的location变量将比我的Rating变量多。
I tried the below code but I am getting an error: 我尝试了以下代码,但出现错误:
Unrated_MraLevel1 <- xyz[(MRA == "0" & xyzLocation)]
error msg : In Ops.factor(MRA== "0", xyz$Location) : '&' not meaningful for factors
错误消息:在Ops.factor(MRA ==“ 0”,xyz $ Location)中:'&'对因子没有意义
Based on your description this is what you want. 根据您的描述,这就是您想要的。
Example data: 示例数据:
dd <- data.frame(location=rep(letters[1:3],each=3),
MRA=c(rep(0,3),1:3,c(2,0,0)))
Aggregate data to find levels where all location
values are equal to zero: 汇总数据以查找所有
location
值均等于零的级别:
a <- aggregate(MRA~location,dd,FUN=function(x) all(x==0))
Extract the levels to keep: 提取要保留的级别:
keep.levels <- a$location[a$MRA]
Subset the data set: 子集数据集:
dd[dd$location %in% keep.levels,]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.