Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select Columns based on Values in Column

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 |
like image 684
user3579222 Avatar asked Jan 26 '26 00:01

user3579222


1 Answers

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))
like image 131
akrun Avatar answered Jan 28 '26 14:01

akrun