3

I am using tidyverse and I am aware of the filter method which allows to filter all rows that have a value that matches a specific condition such as the following. It filters for all rows that have a value between 0 and 3 in at least on column.

filter_all(any_vars(. > 0 & .<3)) 

How can I do the same on column basis? If my tibble looks like the following I want to write a select that returns all columns that have in at least one row a value greater than 4 (should return columns B,C)

    | A | B | C |
    -------------
    | 1 | 1 | 2 |
    | 2 | 5 | 1 |
    | 3 | 6 | 9 |

1 Answer 1

12

We can use select with any

library(dplyr)
df1 %>%
      select_if(~ any(. > 4))

-output

#  B C
#1 1 2
#2 5 1
#3 6 9

Or use where as in the new version

df1 %>%
    select(where(~ any(. > 4)))
#  B C
#1 1 2
#2 5 1
#3 6 9

In base R, this can be done with Filter

Filter(function(x) any(x > 4), df1)

Or with sapply

df1[sapply(df1, function(x) any(x > 4))]

or with colSums

df1[colSums(df1 >4) > 0]     

data

df1 <- data.frame(A = 1:3, B = c(1, 5, 6), C = c(2, 1, 9))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.