使用如下所示的数据框,需要在每行的列之间聚合(平均值)。
> df <- data.frame(
id = c(1,2),
loc=c("loc1", "loc2"),
val1=c(10,20), val2=c(190,NA), val3=c(NA, 10)
)
> df
id loc val1 val2 val3
1 1 loc1 10 190 NA
2 2 loc2 20 NA 10
按列进行常规聚合可以完成
df %>%
group_by(id, loc) %>%
summarise(mean_val1 = mean(val1))
如何按行计算val1, val2, val3
的平均值?所需的输出如下。
id loc vals_mean
1 1 loc1 100
2 2 loc2 15
答案 0 :(得分:4)
您的数据不整齐。首先使用gather
:
library(tidyverse)
df %>%
gather(key = "key", value = "value", val1, val2, val3) %>%
group_by(id, loc) %>%
summarize(mean = mean(value, na.rm = T))
# id loc mean
# <dbl> <fctr> <dbl>
#1 1 loc1 100
#2 2 loc2 15
如果您希望将数据框保持为非格式,rowwise
是您的朋友:
df %>%
rowwise() %>%
mutate(mean = mean(c(val1, val2, val3), na.rm = TRUE))
## A tibble: 2 × 6
# id loc val1 val2 val3 Mean
# <dbl> <fctr> <dbl> <dbl> <dbl> <dbl>
#1 1 loc1 10 190 NA 100
#2 2 loc2 20 NA 10 15