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 |
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]
df1 <- data.frame(A = 1:3, B = c(1, 5, 6), C = c(2, 1, 9))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With