Reputation: 53
I have a dataframe with 51 obs. of 20 variables called test
. Like below:
X1 P1 P2 P3
A 1 0 5
B 3 9 0
C 5 0 0
D 1 0 0
I want to remove all rows where there are values under or equal to 1. So the resulting dataframe would only have A,B,C rows. I thought I cracked it using the below command:
test2 <- test %>% filter_all(any_vars(. > 2))
I also tried
test2 <- test %>% select(X1, P1:P3) %>% filter_all(any_vars(. > 2).
This doesn't remove any of the rows even though I can see rows have values under or equal to 1.
Any help would be appreciated
Upvotes: 1
Views: 414
Reputation: 389135
In base R, you can take help of rowSums
-
res <- test[rowSums(test[-1] > 2) > 0, ]
res
# X1 P1 P2 P3
#1 A 1 0 5
#2 B 3 9 2
#3 C 5 0 0
Upvotes: 0
Reputation: 21938
You can use the following solution:
library(dplyr)
df %>%
filter(if_all(everything(), ~ .x > 1))
With a modified data set it returns:
X1 P1 P2 P3
1 B 3 9 2
Data
structure(list(X1 = c("A", "B", "C", "D"), P1 = c(1L, 3L, 5L,
1L), P2 = c(0L, 9L, 0L, 0L), P3 = c(5L, 2L, 0L, 0L)), class = "data.frame", row.names = c(NA,
-4L))
Here is another way of doing this with purrr::pmap
:
pmap
is used for row-wise operations, here we capture each row of our data set in every iteration with c(...)
while excluding the first variable X1
1
df %>%
filter(pmap_lgl(df[-1], ~ all(c(...) > 1)))
X1 P1 P2 P3
1 B 3 9 2
Upvotes: 1