简体   繁体   English

如何在R中使用多个代码在一个函数中应用“sapply”?

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

I am a new R user. 我是新的R用户。 I have a simple sapply function example for calculating mean and sd for a splitted data frame. 我有一个简单的sapply函数示例,用于计算sapply数据帧的meansd My data contains half hourly wind speed with direction. 我的数据包含半小时风速和方向。 I want to know daily Weibull distribution for my study for 13 years. 我想知道我的研究每日Weibull分发13年。 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: 我用于split-apply查看日期的示例R代码是:

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: 用于计算形状和比例参数的Weibull分布代码是:

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? 如何组合这两组R代码来每天计算Weibull参数并存储在矩阵中?

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? 如果组合这两组R代码,我应该在set1 <- createSet(height=10, v.avg=ballarat_alldata[,2], dir.avg=ballarat_alldata[,3])改变风速和方向范围吗?

It seems as though you have 2 separate problems here: 1) aggregating your data 2) calculating Weibull parameters. 看起来好像你有两个不同的问题:1)聚合你的数据2)计算Weibull参数。 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 如果你给我一些关于如何计算参数的提示,你也可以使用plyr库中的summarise ,类似于

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

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM