Select columns containing a value above a threshold

1.7k Views Asked by At

I have the following dataset

df<-data.frame(c(1,2,1),c(2,1,3), c(1,3,4))

let say that I want to select the columns which contain a value equal or higher than 3 (i.e columns 2 and 3)

I managed to to find the logical argument using df >= 3 and to index with which(df>=3) but I struggle selecting the columns.

1

There are 1 best solutions below

0
On BEST ANSWER

Filter seems like a good option here:

df <- data.frame(x = c(1,2,1), y = c(2,1,3), z = c(1,3,4))

Filter(
  function(x) max(x) >= 3,
  df
  )