[英]How to use dplyr to filter rows where value in a specific column is 1 and all the rest are 0?
Using dplyr
functions, I want to remove rows in which only column b
equals 1
and the rest of columns are all 0
.使用
dplyr
函数,我想删除仅b
列等于1
且列的rest均为0
的行。
Although I can do this:虽然我可以这样做:
library(dplyr, warn.conflicts = FALSE)
trb <-
tribble(~a, ~b, ~c,
1, 1, 1,
1, 1, 0,
1, 0, 1,
0, 1, 0, # <~~~ remove this
0, 0, 0,
0, 1, 0 # <~~~ remove this
)
trb %>%
filter(!(b == 1 & a == 0 & c == 0))
#> # A tibble: 4 x 3
#> a b c
#> <dbl> <dbl> <dbl>
#> 1 1 1 1
#> 2 1 1 0
#> 3 1 0 1
#> 4 0 0 0
I'm looking for a more scalable solution to account for data such as:我正在寻找一种更具可扩展性的解决方案来处理以下数据:
trb_2 <-
tibble::tribble(
~a, ~b, ~c, ~d, ~e, ~f, ~g, ~h, ~i, ~j, ~k, ~l, ~m, ~n, ~o, ~p, ~q, ~r, ~s, ~t, ~u, ~v, ~w, ~x, ~y, ~z,
0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0,
1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0,
0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0,
0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0,
0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1
)
In trb_2
I still want to remove the rows in which b
equals 1
and all the rest are 0
.在
trb_2
我仍然想删除b
等于1
并且所有 rest 都是0
的行。
Is there a scalable way to achieve this using dplyr::filter()
?是否有使用
dplyr::filter()
实现此目的的可扩展方法?
Yes, using the new helper function dplyr::if_all()
you can do this for no matter how many columns you have:是的,使用新的助手 function
dplyr::if_all()
无论你有多少列,你都可以这样做:
trb %>%
filter(!(b == 1 & if_all(-b, ~ .x == 0)))
Result:结果:
# A tibble: 4 x 3
a b c
<dbl> <dbl> <dbl>
1 1 1 1
2 1 1 0
3 1 0 1
4 0 0 0
Breakdown of ,(b == 1 & if_all(-b. ~ .x == 0))
: ,(b == 1 & if_all(-b. ~ .x == 0))
的细分:
b == 1
will match rows where b is 1 b == 1
将匹配 b 为 1 的行if_all(-b, ~.x == 0)
will match rows where all columns except b are exactly 0 if_all(-b, ~.x == 0)
将匹配除 b 之外的所有列都为 0 的行,(b == 1 & if_all(-b. ~ .x == 0))
combines these two expressions and removes the rows where both are true ,(b == 1 & if_all(-b. ~ .x == 0))
结合这两个表达式并删除两者都为真的行trb %>% filter(b != 1 | rowSums(. == 1) != 1) # # A tibble: 4 x 3 # a b c # <dbl> <dbl> <dbl> # 1 1 1 1 # 2 1 1 0 # 3 1 0 1 # 4 0 0 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.