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0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0.6, 0.2, 2.6, 3, 0.4, 0, 0, 0, 0, 0, 0, 0,
0.2, 0, 0, 0, 0.2, 0, 0, 0, 0, 0.8, 0.2, 0.2, 0, 0, 0, 0, 0,
0.2, 0, 0.2, 0, 0.4, 0.4, 0.8, 0.8, 1.2, 1, 0.6, 0.6, 0.4, 0.2,
0.2, 0.4, 0.2, 0.2, 0.4, 0.2, 0.8, 0, 0, 0.2, 0, 0, 0.2, 0, 0.2,
0, 0.2, 0, 0.2, 0.2, 0, 0, 0, 0)), .Names = c("X2005.hourly", "X2006.hourly",
"X2007.hourly"), class = "data.frame", row.names = c(NA, -744L))
以及我之前使用的功能:
myfun<- function(x) {
y<-as.matrix(sapply(x, as.numeric))
#Aggregating hourly data to 6,12 and 24 hourly data
h<-sapply(c(1, 6, 12, 24), function(hrs) colSums(matrix(y, ncol=length(x)/hrs)))
hours<-sapply(list(h[[1]], h[[2]], h[[3]], h[[4]]), function(qq) list(qq))
names(hours)<-c("hourly", "h.6", "h.12", "h.24")
#Calculating means of 1, 6, 12 and 24 hourly data
means<-sapply(list(hours[[1]], hours[[2]], hours[[3]], hours[[4]]), function(ss) mean(ss, na.rm = T))
#means of 1, 6, 12 and 24 hourly data
mean1<-means[[1]]
mean6<-means[[2]]
mean12<-means[[3]]
mean24<-means[[4]]
meanlist<-list(mean1, mean6, mean12, mean24)
names(meanlist) <- c("mean1", "mean6", "mean12", "mean24")
return(meanlist)
}
我做什么:
我正在使用具有myfun
功能的lapply
,并且每年分别获得结果。而且这里没有麻烦。
results<-lapply(mydata, myfun)
我想要的东西:
如您所见,h.24
函数中names(hours)
内有myfun
项。为了计算24 hourly data
的平均值,函数使用从h.24
数据中聚合的hourly
数据。但是我想使用外部的24小时daily
数据而不是h.24
。在下面的数据中,daily
分别有2005,2006,2007
个值
adddata<-structure(list(X2005.daily = c(0, 0, 3.4, 21.4, 1.8, 1.4, 0,
0, 0, 0, 0, 0, 0, 0, 0, 8.6, 1.4, 0, 0, 0, 0, 0.6, 7, 6.4, 21.8,
3.2, 0, 0, 0.6, 11.2, 0), X2006.daily = c(0, 8.6, 11.2, 8.4,
0, 0, 0, 0, 0, 7.8, 7.6, 1.2, 10.2, 7, 0, 0, 1.6, 7.6, 23.6,
3.2, 0, 9, 31.4, 10, 2.2, 1.6, 0, 4.8, 10, 1.2, 4.6), X2007.daily = c(0.6,
9.4, 4.2, 7.2, 15, 15, 0.4, 0, 8.6, 0, 0.2, 0.8, 0, 0.6, 0.6,
1.4, 8.2, 0.2, 0, 0, 0, 0, 5, 1.6, 4, 5.2, 0, 0, 7, 4.2, 7.2)), .Names = c("X2005.daily",
"X2006.daily", "X2007.daily"), class = "data.frame", row.names = c(NA,
-31L))
简而言之::我想每年添加外部daily
数据,然后我想计算1、6、12小时值的均值以及增加的{{1 }}每年的数据。因此,daily
的结果将是相同的,但mean1, mean6, mean12
将是增加的每日值的平均值。
答案 0 :(得分:1)
如果我正确理解了您的问题,则应该可以:
results <- do.call(cbind,results) #convert your list to a data.frame for better overview
colnames(results) <- c("X2005","X2006","X2007")
results <- rbind(results,meanDaily=colMeans(adddata)) #rbind the colMeans of you adddata
results
X2005 X2006 X2007
mean1 0.1193548 0.2336022 0.1430108
mean6 0.716129 1.401613 0.8580645
mean12 1.432258 2.803226 1.716129
mean24 2.864516 5.606452 3.432258
meanDaily 2.864516 5.574194 3.43871
评论中请求的其他答案:
library(zoo)
myfun <- function(df_hourly,df_daily)
{
DF <- sapply(df_hourly,function(col){c(mean(rollapply(col,width=1,sum,by=1,align="left")),
mean(rollapply(col,width=6,sum,by=6,align="left")),
mean(rollapply(col,width=12,sum,by=12,align="left")),
mean(rollapply(col,width=24,sum,by=24,align="left")))})
DF <- rbind(DF,colMeans(adddata))
colnames(DF) <- c("yr2005","yr2006","yr2007")
rownames(DF) <- c("mean.1hr","mean.6hr","mean.12hr","mean.24hr","mean.daily")
return(DF)
}
myfun(mydata,adddata)
yr2005 yr2006 yr2007
mean.1hr 0.1193548 0.2336022 0.1430108
mean.6hr 0.7161290 1.4016129 0.8580645
mean.12hr 1.4322581 2.8032258 1.7161290
mean.24hr 2.8645161 5.6064516 3.4322581
mean.daily 2.8645161 5.5741935 3.4387097