Search code examples
rskewgroup-summarieskurtosis

Calculate skew and kurtosis by year in R


I have a table that looks like this:

start_table <- data.frame("Water_Year" =  c("1903", "1903", "1904", "1904"), "X" = c(13, 11, 12, 
15), "Day" = c(1, 2, 1, 2))

(The 'Day' column is not involved in my skew & kurtosis calculation, it is just in my table)

I would like a table that calculates the skew and kurtosis values grouped by year:

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

I can't figure out how to group it by year to perform these calculations.


Solution

  • An option is group_by/summarise

    library(dplyr)
    library(moments)
    start_table %>% 
       group_by(Water_Year) %>%
       summarise(Skew = skewness(X), Kurtosis = kurtosis(X))