简体   繁体   中英

Is there a way to combine dplyr::filter_at results? (filtering many variables at once)

I have the following dataframe:

  A B C D
1 1 2 3 T
2 1 5 5 F
3 1 1 1 T
4 5 5 5 T
5 5 5 5 T

I'm trying to remove rows that contain all of the same number (eg, all 5s, all 1s) for variables A through C (in my actual data, I have many more variables between A and C ). I can filter rows with all 5s by doing this:

library(dplyr)

A <- c(1, 1, 1, 5, 5)
B <- c(2, 5, 1, 5, 5)
C <- c(3, 5, 1, 5, 5)
D <- c(2, 2, 2, 2, 2)

df <- data.frame(A, B, C, D)

df %>%
  filter_at(.vars = 1:3, .vars_predicate = all_vars(. == 5))


  A B C D
1 5 5 5 T
2 5 5 5 T

Is there a way to chain another filter_at() so that I can do the same for rows with all 1s? The ideal output would be this:

  A B C D
1 5 5 5 T
2 5 5 5 T
3 1 1 1 T

I've tried using logical operators within all_vars() , but it doesn't yield the correct result. In the resulting dataframe below, we get rows that contain both 5s and 1s.

df %>%
  filter_at(.vars = 1:3, .vars_predicate = all_vars(. == 5 | . == 1))

  A B C D
1 1 5 5 F
2 1 1 1 T
3 5 5 5 T
4 5 5 5 T

Again, I'm trying to avoid manually filtering each variable (eg, filter(A == 1 & B == 1 ... ) ) because I have many dozens of other columns.

Any alternative approaches or package suggestions are most welcome.

Old-style R programming using logical indexing in the i -position with [ :

df[ apply( df[1:3], 1, function(x){sum(x==5)==3 || sum(x==1)==3}), ]
  A B C D
3 1 1 1 2
4 5 5 5 2
5 5 5 5 2

df[apply( df[1:3], 1, function(x){all(x==5) || all(x==1)}), ]
  A B C D
3 1 1 1 2
4 5 5 5 2
5 5 5 5 2

You need a function that can evaluate the values rowwise. There's a few choices, but one option is:

library(dplyr)

df %>%
  filter_at(1:3, ~ .x %in% c(1,5) & do.call(pmin, df[1:3]) == do.call(pmax, df[1:3])) 

  A B C D
1 1 1 1 2
2 5 5 5 2
3 5 5 5 2 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM