kmmoon100
2013-Dec-09 09:07 UTC
[R] How can I apply “Sapply” in R with multiple codes in one function?
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.
Jeff Newmiller
2013-Dec-09 16:05 UTC
[R] How can I apply “Sapply” in R with multiple codes in one function?
There is a no-homework policy on this mailing list (see the Posting Guide
mentioned at the bottom of every email). If this is not assigned homework then
at the very least you need to understand R and your problem well enough to
provide a reproducible example [1] before we can communicate about your problem
on this list.
As a start, you should be able to execute each line of each of your examples one
at a time. R is interactive and scriptable, so you can always give one line at a
time to to R and examine the results. Some results may be printed to the console
and then forgotten, while others are stored in objects for use in subsequent
lines of code. Try reading and following along with the Introduction to R
document that comes with R to learn how R works.
If you do all this, you should come to understand that most of the code in your
weibull example seems to be about cleaning up the input data in memory, but I
don't know where the functions it references are so you need to get more
input from whoever supplied the example about how to use it. It may be as simple
as loading a package at the beginning of the code. You would not normally clean
the data repeatedly, so most of that code should probably precede your use of
sapply.
The weibull function returns an object, and your example code just prints it and
discards it. If you want to transfer information from that object into a vector
that sapply can bind with a bunch of other vectors to make a matrix, then you
will have to assign it into an object and extract those values one at a time as
you create that vector.
I highly recommend using the str function on the various objects created by each
line of example code at the R console in between execution of the example lines.
Use the help facility to learn more:
?str
[1]
http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live
Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
kmmoon100 <k.moon at student.unimelb.edu.au>
wrote:>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.
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.