I have a dataset and I want to find all rows where a specific value occurs in ANY COLUMN.
Let's use mtcars dataset - i want to find all rows where the value of '4' occurs...
library(dplyr)
mtcars %>% filter( any( %in% 4))???
I have a dataset and I want to find all rows where a specific value occurs in ANY COLUMN.
Let's use mtcars dataset - i want to find all rows where the value of '4' occurs...
library(dplyr)
mtcars %>% filter( any( %in% 4))???
You can use rowSums
to construct the subset condition:
dplyr::filter(mtcars, rowSums(mtcars == 4) != 0)
# mpg cyl disp hp drat wt qsec vs am gear carb
# 1 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
# 2 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
# 3 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
# 4 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
# 5 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
# 6 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
# ...