[R] How can I apply “Sapply” in R with multiple codes in one function?

kmmoon100 k.moon at student.unimelb.edu.au
Mon Dec 9 10:07:30 CET 2013


Hello everybody,

I am a new R user..(still;;;). I have a simple "sapply" function example for
calculating mean and sd of splited data scale. 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 splited 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 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 rather than mean and sd, and store in a matrix? I tried many ways
but it doesn't work out well.. (apology to my poor understanding of R!!!)
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?

Thank you.



--
View this message in context: http://r.789695.n4.nabble.com/How-can-I-apply-Sapply-in-R-with-multiple-codes-in-one-function-tp4681852.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list