过滤一列的行,条件是所有其他列的行都是NA,并重复过滤n列。

问题描述 投票:0回答:1

如果这个问题以前被问过,我很抱歉;我不太确定如何表达这个问题,这可能会阻止其他问题在我的搜索中显示出来。

我的情况是,我有一个这样的数据集。

toy <- 
  data.frame(
    Serves_1 = c("yes", NA, "yes", "no", "yes", "no"),
    Serves_2 = c(NA, NA, "no", "no", "no", "yes"),
    Serves_3 = c(NA, "no", "yes", "no", NA, "no"),
    Serves_4 = c(NA, "yes", "yes", "no", "yes", "no")
  )
toy

我想确定有多少行的某一列是非NA,而其他列都是NA。所以以列Serves_1为例。

toy %>%
  filter(
    !is.na(Serves_1) &
      is.na(Serves_2) &
      is.na(Serves_3) &
      is.na(Serves_4)
  ) %>%
  nrow

有一行Serves_1的值是非NA的 同时,所有其他列的值都是NA。

这段代码很好用,但我需要对每一列重复这个过程。我可以直接将每列的感叹号向下移动。但在我的真实数据集中,我必须为20多列做这个操作。

有没有更有效的方法(最好是使用dplyr)?

r filter dplyr na
1个回答
0
投票

你可以使用 rowSums:

library(dplyr)
toy <- 
  data.frame(
    Serves_1 = c("yes", NA, "yes", "no", "yes", "no"),
    Serves_2 = c(NA, NA, "no", "no", "no", "yes"),
    Serves_3 = c(NA, "no", "yes", "no", NA, "no"),
    Serves_4 = c(NA, "yes", "yes", "no", "yes", "no")
  ) %>% 
  mutate(na_sum = rowSums(is.na(.)))

这样你就可以得到:

  Serves_1 Serves_2 Serves_3 Serves_4 na_sum
1      yes     <NA>     <NA>     <NA>      3
2     <NA>     <NA>       no      yes      2
3      yes       no      yes      yes      0
4       no       no       no       no      0
5      yes       no     <NA>      yes      1
6       no      yes       no       no      0

你可以在以下地方过滤行 na_sum ==3,得到所有的行,其中有一个值不是NA,其余的都是。

toy %>% 
  filter(na_sum ==3)

我们得到:

  Serves_1 Serves_2 Serves_3 Serves_4 na_sum
1      yes     <NA>     <NA>     <NA>      3

0
投票

附加选项

sum(apply(toy, 1, function(x) (length(x) - 1 == sum(is.na(x)))))
© www.soinside.com 2019 - 2024. All rights reserved.