Sarah
Sarah

Reputation: 463

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.

Upvotes: 3

Views: 4123

Answers (3)

tmfmnk
tmfmnk

Reputation: 40171

You can also define the skewness/kurtosis functions:

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
}

Then, to apply it in 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

Upvotes: 6

M--
M--

Reputation: 29173

Using fBasics with 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

Upvotes: 2

akrun
akrun

Reputation: 887741

An option is group_by/summarise

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

Upvotes: 6

Related Questions