Subset a dataframe based on a single condition applied to multiple columns

痞子三分冷 提交于 2019-12-07 08:38:59

问题


I've had a look through the existing subset Q&A's on this site and couldn't quite find what I was looking for.

I want to subset a data frame based on one condition (e.g. if the value is below 5). However, I only want the rows where the value in all of the columns is below 5.

For example using the iris dataset - I would like to select all the rows where columns 1-3 all have values below 5.

subdata <- iris[which(iris[,1:3]<5),]

This doesn't do it for me. I get lots of NA rows at the bottom of the subset data.

Any help much appreciated!


回答1:


Try

subdata <- iris[apply(iris[,1:3] < 5, 1, all),]


来源:https://stackoverflow.com/questions/15068408/subset-a-dataframe-based-on-a-single-condition-applied-to-multiple-columns

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