user11168641
user11168641

Reputation:

How to select column values based on a greater than condition in row values

I want to select columns from a data frame if any of the values in the column is greater than 3.

In this mtcars example, this criterion will drop vs and am, as there is no value greater than 3 in those columns.

                 mpg cyl  disp  hp drat    wt  qsec vs am gear carb
Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2

Upvotes: 3

Views: 9853

Answers (3)

NelsonGon
NelsonGon

Reputation: 13319

With purrr:

mtcars[,purrr::map_lgl(mtcars,~any(.x>3))]

Output(truncated):

   #                      mpg cyl  disp  hp drat    wt  qsec gear carb
   # Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46    4    4
   # Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02    4    4
   # Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61    4    1
   # Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44    3    1
   # Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02    3    2

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 389012

We can create a logical vector by comparing the dataframe with 3 and then take sum of columns using colSums and select only those columns which has at least one value greater than 3 in it.

mtcars[colSums(mtcars > 3) > 0]

#                     mpg cyl  disp  hp drat    wt  qsec gear carb
#Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46    4    4
#Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02    4    4
#Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61    4    1
#Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44    3    1
#....

Variation using sapply

mtcars[sapply(mtcars, function(x) any(x > 3))]

Upvotes: 5

Ben Bolker
Ben Bolker

Reputation: 226257

I think

## find max val for each column
maxcolval <- apply(mtcars,MARGIN=2,FUN=max)
## select columns
mtcars[maxcolval>3]

does what you want. If you want a tidyverse solution (since you have dplyr in the tags),

library(dplyr) 
mtcars %>% select_if(~max(.)>3)

Upvotes: 0

Related Questions