0

I have a file with daily data for multiple years. I would like to calculate the skewness for every month for a variable. I have multiple files as data.frame. I guess a similar question might have been answered before - the answer must lie somewhere in the moments or e1071 package, but I couldn't figure it out yet.

Is there a skewness function in R than can help me that is similar to one of the formulas for mean and standard deviation below?:

 # take average of a column per month
 MEAN1 <- ddply(FILENAME, c("Month"), function(x) colMeans(x[c("variable1",   "variable2")]))

 #take standard deviation of all columns per month
 sd1 <- ddply(FILENAME, .(Month), colwise(sd)) 
#whereafter I delete the non-relevant columns 

I would like the final result to be something like:

month variable1skewness
01 1.5
02 2.1
03 2.4
04 2.2
05 2.6
06 1.8
07 3.3
08 4.1
09 3.4
10 3.2
11 4.1
12 1.9

All help is appreciated!! Thanks

T. BruceLee
  • 501
  • 4
  • 16
  • Could you please give a reproducible example (i.e. use built-in or publicly available data)? http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Hack-R Jun 22 '16 at 13:20

1 Answers1

0

The tidy.data.frame method in broom will calculate skewness, kurtosis, and a bunch of other values.

Look at, for example

library(broom)
tidy(mtcars)

Not having a reproducible example, I've had to fudge an example:

library(magrittr)
library(dplyr)
library(broom)

split(mtcars[, c("mpg", "gear")], mtcars$gear) %>%
  lapply(function(x) cbind(gear = x$gear[1], tidy(x))) %>%
  bind_rows() %>%
  filter(column == "mpg") %>%
  select(gear, column, mean, sd, skew, kurtosis) 

There is probably a cleaner way to do it with dplyr::do, but I'm not that clever.

Benjamin
  • 16,897
  • 6
  • 45
  • 65