Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 498
JPD Avatar asked Dec 26 '22 08:12

JPD


1 Answers

Try

subdata <- iris[apply(iris[,1:3] < 5, 1, all),]
like image 115
QkuCeHBH Avatar answered Jan 31 '23 10:01

QkuCeHBH