Subset a data frame using OR when the column contains a factor

泄露秘密 提交于 2019-12-10 12:30:18

问题


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.

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.


回答1:


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[ fake$nm %in% c("a","b"), ]



回答2:


Another approach would be to use subset() and write

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


来源:https://stackoverflow.com/questions/5680819/subset-a-data-frame-using-or-when-the-column-contains-a-factor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!