Вы можете попробовать это
library(tidyverse)
df1 %>%
group_by(gp, id, name) %>%
summarise_all(sum, na.rm = TRUE) %>%
summarise_all(na_if, 0)
# A tibble: 3 x 7
# Groups: gp [?]
# gp id name a1 a2 a3 a4
# <fct> <dbl> <fct> <dbl> <dbl> <dbl> <dbl>
#1 gp1 1. A 0.400 NA 1.20 NA
#2 gp1 2. B NA 1. NA 1.
#3 gp1 3. C 0.300 NA NA 1.
Не будет никаких NA
с, но 0
с в конечном выводе, отсюда второй вызов summarise_all
.Я предполагаю, что в столбцах a1
до a4
нет 0
s.
Вот решение для случая, когда в вашем начальном наборе данных есть 0
s.
sum_NA <- function(x) {
if(all(is.na(x))) {
NA
} else {
sum(x, na.rm = TRUE)
}
}
df2 %>%
group_by(gp, id, name) %>%
summarise_all(sum_NA)
# A tibble: 3 x 7
# Groups: gp, id [?]
# gp id name a1 a2 a3 a4
# <fct> <dbl> <fct> <dbl> <dbl> <dbl> <dbl>
#1 gp1 1. A 0. NA 1.20 NA
#2 gp1 2. B NA 0. NA 1.
#3 gp1 3. C 0.300 NA NA 1.
данные
df1 <- structure(list(gp = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "gp1", class = "factor"), id = c(1, 1, 2, 2, 2, 2, 3, 3, 3), name = structure(c(1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("A", "B", "C"), class = "factor"), a1 = c(0.4, NA, NA, NA, NA, NA, 0.3, NA, NA), a2 = c(NA, NA, NA, 1, NA, NA, NA, NA, NA), a3 = c(NA, 1.2, NA, NA, NA, NA, NA, NA, NA), a4 = c(NA, NA, 1, NA, NA, NA, NA, NA, 1)), .Names = c("gp", "id", "name", "a1", "a2", "a3", "a4"), row.names = c(NA, -9L), class = "data.frame")
df2 <- structure(list(gp = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "gp1", class = "factor"), id = c(1, 1, 2, 2, 2, 2, 3, 3, 3), name = structure(c(1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L), .Label = c("A", "B", "C"), class = "factor"), a1 = c(0.0, NA, NA, NA, NA, NA, 0.3, NA, NA), a2 = c(NA, NA, NA, 0, NA, NA, NA, NA, NA), a3 = c(NA, 1.2, NA, NA, NA, NA, NA, NA, NA), a4 = c(NA, NA, 1, NA, NA, NA, NA, NA, 1)), .Names = c("gp", "id", "name", "a1", "a2", "a3", "a4"), row.names = c(NA, -9L), class = "data.frame")