我如何申请“申请"?在一个函数中具有多个代码的R中? [英] How can I apply "sapply" in R with multiple codes in one function?

查看:57
本文介绍了我如何申请“申请"?在一个函数中具有多个代码的R中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的R用户.我有一个简单的sapply函数示例,用于为分割的数据帧计算meansd.我的数据包含每小时半小时的风速和方向.我想知道13年的日常Weibull分布.这就是为什么我的数据集会根据时间进行拆分的原因.

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.

我的数据如下:

    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  

我需要拆分应用程序查看的示例R代码是:

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)))
                     })

用于计算形状和比例参数的Weibull分布代码为:

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)

如何组合这两组R代码每天计算出Weibull参数并存储在矩阵中?

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

我尝试了很多方法,但是效果不佳.
如果将这两组R代码组合在一起,是否也应该在set1 <- createSet(height=10, v.avg=ballarat_alldata[,2], dir.avg=ballarat_alldata[,3])中更改风速和风向范围?

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)汇总数据2)计算Weibull参数.对于第一个问题,我可以推荐类似的内容:

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

如果您给我更多有关如何计算参数的提示,您还可以使用plyr库中的summarise,类似

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

希望这会有所帮助.

这篇关于我如何申请“申请"?在一个函数中具有多个代码的R中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆