r-使用summary和来自dplyr的cross按组计数几个列的非"NA"



我想使用dplyr中的summarizeacross来通过我的分组变量计算非NA值的数量。例如,使用以下数据:

library(tidyverse)  
d <- tibble(ID = c(1, 1, 1, 2, 2, 2, 3, 3, 3),
Col1 = c(5, 8, 2, NA, 2, 2, NA, NA, 1),
Col2 = c(NA, 2, 1, NA, NA, NA, 1, NA, NA),
Col3 = c(1, 5, 2, 4, 1, NA, NA, NA, NA))  
# A tibble: 9 x 4
ID  Col1  Col2  Col3
<dbl> <dbl> <dbl> <dbl>
1     1     5    NA     1
2     1     8     2     5
3     1     2     1     2
4     2    NA    NA     4
5     2     2    NA     1
6     2     2    NA    NA
7     3    NA     1    NA
8     3    NA    NA    NA
9     3     1    NA    NA

使用类似的解决方案:

d %>%
group_by(ID) %>%
summarize(across(matches("^Col[1-3]$"),
#function to count non-NA per column per ID
))

结果如下:

# A tibble: 3 x 4
ID  Col1  Col2  Col3
<dbl> <dbl> <dbl> <dbl>
1     1     3     2     3
2     2     2     0     2
3     3     1     1     0

我希望这就是您想要的:

library(dplyr)
d %>%
group_by(ID) %>%
summarise(across(Col1:Col3, ~ sum(!is.na(.x)), .names = "non-{.col}"))
# A tibble: 3 x 4
ID `non-Col1` `non-Col2` `non-Col3`
<dbl>      <int>      <int>      <int>
1     1          3          2          3
2     2          2          0          2
3     3          1          1          0

或者,如果你想通过共享字符串来选择列,你可以使用这个:

d %>%
group_by(ID) %>%
summarise(across(contains("Col"), ~ sum(!is.na(.x)), .names = "non-{.col}"))

最新更新