How can I apply "sapply" in R with multiple codes in one function?

156 views Asked by At

I am a new R user. I have a simple sapply function example for calculating mean and sd for a splitted data frame. My data contains half hourly wind speed with direction. I want to know daily Weibull distribution for my study for 13 years. That is why my dataset is splitted based on time.

My data looks like this:

    Time             windspeed direction    Date            day_index
1   24/07/2000 13:00    31       310    2000-07-24 13:00:00 2000_206
2   24/07/2000 13:30    41       320    2000-07-24 13:30:00 2000_206
3   24/07/2000 14:30    37       290    2000-07-24 14:30:00 2000_206
4   24/07/2000 15:00    30       300    2000-07-24 15:00:00 2000_206
5   24/07/2000 15:30    24       320    2000-07-24 15:30:00 2000_206
6   24/07/2000 16:00    22       330    2000-07-24 16:00:00 2000_206
7   24/07/2000 16:30    37       270    2000-07-24 16:30:00 2000_206  

The example R code I have for the split-apply to look over the days is:

my.summary <- sapply(split(ballarat_alldata[1:200, ],
                           ballarat_alldata$day_index[1:200]),
                     function(x) {
                         return(c(my.mean=mean(x$windspeed),
                                  my.sd=sd(x$windspeed)))
                     })

The Weibull distribution code to calculate shape and scale parameters is:

set1 <- createSet(height=10,
                  v.avg=ballarat_alldata[,2],
                  dir.avg=ballarat_alldata[,3])
time_ballarat <- strptime(ballarat_alldata[,1], "%d/%m/%Y %H:%M")
ballarat <- createMast(time.stamp=time_ballarat, set1)
ballarat <- clean(mast=ballarat)
ballarat.wb <- weibull(mast=ballarat, v.set=1, print=FALSE)

How can I combine these two set of R codes to calculate Weibull parameters each day and store in a matrix?

I tried many ways but it doesn't work out well.
If these two sets of R codes are combined, should I change wind speed and direction range in set1 <- createSet(height=10, v.avg=ballarat_alldata[,2], dir.avg=ballarat_alldata[,3]) too?

1

There are 1 answers

0
Jack Ryan On

It seems as though you have 2 separate problems here: 1) aggregating your data 2) calculating Weibull parameters. For the first question I can recommend something like:

library(plyr)
Wind <- ddply(Wind, .(as.Date(Date)), transform, 
Wind.mean = mean(windspeed), Wind.sd = sd(windspeed))
#       windspeed direction      Date2    Time2 day_index Wind.mean  Wind.sd
#       1        31       310 2000-07-24 13:00:00  2000_206  36.33333 5.033223
#       2        41       320 2000-07-24 13:30:00  2000_206  36.33333 5.033223
#       3        37       290 2000-07-24 14:30:00  2000_206  36.33333 5.033223
#       4        30       300 2000-07-25 15:00:00  2000_206  28.25000 6.751543
#       5        24       320 2000-07-25 15:30:00  2000_206  28.25000 6.751543
#       6        22       330 2000-07-25 16:00:00  2000_206  28.25000 6.751543
#       7        37       270 2000-07-25 16:30:00  2000_206  28.25000 6.751543

If you give me a little bit more of a hint on how you are calculating the parameters you can also use the summarise from the plyr library, something like

ddply(Wind, .(Date2), summarise, rweibull(# I'm not sure what goes here

Hope this helps.