我有一个看起来像这样的表:
start_table <- data.frame("Water_Year" = c("1903", "1903", "1904", "1904"), "X" = c(13, 11, 12,
15), "Day" = c(1, 2, 1, 2))
(“天”列不参与我的偏度和峰度计算,它只是在我的表中)
我想要一个表,用于按年份分组的偏斜度和峰度值:
end_table <- data.frame("Water_Year" = c("1903", "1904"), "Skew" = c("skew_number_here",
"skew_number_here"), "Kurtosis" = c("kurtosis_number_here", "kurtosis_number_here"))
我不知道如何按年份将其分组以执行这些计算。
最佳答案
一个选项是group_by/summarise
library(dplyr)
library(moments)
start_table %>%
group_by(Water_Year) %>%
summarise(Skew = skewness(X), Kurtosis = kurtosis(X))