You can use the tidyverse
approach.
require(tidyverse)
#Sample data
dat <- data.frame(group = rep(c("a", "b", "c", "d", "g"), 3),
y = rep(c(1, NA, 2, NA, 3), 3))
dat %>%
group_by(group) %>%
summarise(sumNA = sum(is.na(y)))
Output:
group sumNA
<fct> <int>
1 a 0
2 b 3
3 c 0
4 d 3
5 g 0
Edit
However, if you have more than one column, you can use summarize_all
(or summarize_at
if you'd like to specify the columns; thank you @ bschneidr for the comment):
#Sample data
set.seed(123)
dat <- data.frame(group = sample(letters[1:4], 10, replace = T),
x = sample(c(1,NA), 10, replace = T),
y = sample(c(1,NA), 10, replace = T),
z = sample(c(1, NA), 10, replace = T))
dat %>%
group_by(group) %>%
summarize_all(.funs = funs('NA' = sum(is.na(.))))
# A tibble: 4 x 4
group x_NA y_NA z_NA
<fct> <int> <int> <int>
1 a 1 1 0
2 b 3 2 2
3 c 0 1 1
4 d 1 4 2
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…