3

我有一个看起来像这样的表:

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"))

我不知道如何按年份对其进行分组以执行这些计算。

4

3 回答 3

5

您还可以定义偏度/峰度函数:

kurtosis <- function(x) {  
 m4 <- mean((x - mean(x))^4) 
 kurtosis <- m4/(sd(x)^4) - 3  
 kurtosis
}

skewness <-  function(x) {
 m3 <- mean((x - mean(x))^3)
 skewness <- m3/(sd(x)^3)
 skewness
}

然后,将其应用于base R

aggregate(X ~ Water_Year, 
          FUN = function(x) c(kurtosis = kurtosis(x), skewness = skewness(x)),
          data = start_table)

  Water_Year X.kurtosis X.skewness
1       1903      -2.75       0.00
2       1904      -2.75       0.00
于 2019-10-21T17:56:13.660 回答
3

一个选项是group_by/summarise

library(dplyr)
library(moments)
start_table %>% 
   group_by(Water_Year) %>%
   summarise(Skew = skewness(X), Kurtosis = kurtosis(X))
于 2019-10-21T17:45:31.010 回答
2

fBasics与 一起使用data.table

library(fBasics)
library(data.table)
setDT(start_table)[, .(Skew = skewness(X), Kurtosis=kurtosis(X)), .(Water_Year)][]
#>    Water_Year Skew Kurtosis
#> 1:       1903    0    -2.75
#> 2:       1904    0    -2.75
于 2019-10-21T17:49:49.950 回答