当列包含因子时,使用OR子集数据框

时间:2022-09-09 19:26:32

I would like to make a subset of a data frame in R that is based on one OR another value in a column of factors but it seems I cannot use | with factor values.

我想在R中创建一个数据帧的子集,它基于一列因子中的一个或另一个值,但似乎我不能使用与因子值。

Example:

例:

# fake data
x <- sample(1:100, 9)
nm <- c("a", "a", "a", "b", "b", "b", "c", "c", "c")
fake <- cbind(as.data.frame(nm), as.data.frame(x))
# subset fake to only rows with name equal to a or b
fake.trunk <- fake[fake$nm == "a" | "b", ]

produces the error:

产生错误:

Error in fake$nm == "a" | "b" : 
operations are possible only for numeric, logical or complex types

How can I accomplish this?

我怎么能做到这一点?

Obviously my actual data frame has more than 3 values in the factor column so just using != "c" won't work.

显然,我的实际数据框在因子列中有超过3个值,所以只使用!=“c”将不起作用。

2 个解决方案

#1


24  

You need fake.trunk <- fake[fake$nm == "a" | fake$nm == "b", ]. A more concise way of writing that (especially with more than two conditions) is:

你需要fake.trunk < - 假[假$ nm ==“a”|假$ nm ==“b”,]。一种更简洁的写作方式(特别是有两个以上的条件)是:

fake[ fake$nm %in% c("a","b"), ]

#2


13  

Another approach would be to use subset() and write

另一种方法是使用subset()和write

fake.trunk = subset(fake, nm %in% c('a', 'b'))

#1


24  

You need fake.trunk <- fake[fake$nm == "a" | fake$nm == "b", ]. A more concise way of writing that (especially with more than two conditions) is:

你需要fake.trunk < - 假[假$ nm ==“a”|假$ nm ==“b”,]。一种更简洁的写作方式(特别是有两个以上的条件)是:

fake[ fake$nm %in% c("a","b"), ]

#2


13  

Another approach would be to use subset() and write

另一种方法是使用subset()和write

fake.trunk = subset(fake, nm %in% c('a', 'b'))