简体   繁体   English

如何使用 dplyr 过滤特定列中值为 1 且所有 rest 为 0 的行?

[英]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.

相关问题 如何使用 dplyr::filter() 仅返回值包含一个或多个给定字符串向量的行? - How to use dplyr::filter() to return only rows where the value contains one or more of a given vector of strings? 在使用 dplyr 的特定列值的第一个实例之后过滤 dataframe 中的 R 中的行 - Filter rows in dataframe in R after first instance of specific column value using dplyr dplyr filter()会忽略值为0的行,如何更改 - dplyr filter () ignores the rows with the value 0, how to change that 如何在 dplyr 过滤器 function 中使用列号 - How to use column numbers in the dplyr filter function 如何在dplyr的过滤器中正确使用all? - How to correctly use all in dplyr's filter? dplyr 为所有具有其他列的唯一组合的行过滤值为 0 的列 - dplyr filter columns with value 0 for all rows with unique combinations of other columns 如何使用 num_range 选择在一个特定列中都包含相同前 4 位数字的行? (希望使用 dplyr/tidyverse) - How do I use num_range to select rows which all contain the same first 4 digits in one specific column? (hoping to use dplyr/tidyverse) 使用 dplyr 将所有行保持在 R 中的特定值 - Keep all rows up to a specific value in R using dplyr 如何使用 dplyr 过滤掉数据框中的特定行? - How to Filter out specific rows in a data frame using dplyr? dplyr:如何在过滤器 function 中包含基于其 position 的特定行? - dplyr: how to include specific rows based on their position in filter function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM