【发布时间】:2016-08-06 01:58:15
【问题描述】:
为了避免在 R 中使用 for 循环,我编写了一个函数,该函数在给定来自另一个数据帧的特定行值的情况下返回一个数据帧的平均值。然后我将这个函数传递给 sapply 的行号范围。我的函数有效,但它每秒返回约 2.5 个结果,这并不比使用 for 循环好多少。所以,我觉得我还没有充分利用 apply 系列函数的向量化方面。谁能帮我重新考虑我的方法?这是一个最低限度的工作示例。提前致谢。
#Creating first dataframe
dates<-seq(as.Date("2013-01-01"), as.Date("2016-07-01"), by = 1)
n<-length(seq(as.Date("2013-01-01"), as.Date("2016-07-01"), by = 1))
df1<-data.frame(date = dates,
hour = sample(1:24, n,replace = T),
cat = sample(c("a", "b"), n, replace = T),
lag = sample(1:24, n, replace = T))
#Creating second dataframe
df2<-data.frame(date = sort(rep(dates, 24)),
hour = rep(1:24, length(dates)),
p = runif(length(rep(dates, 24)), min = -20, max = 100))
df2<-df2[order(df2$date, df2$hour),]
df2$cat<-"a"
temp<-df2
temp$cat<-"b"
df2<-rbind(df2,temp)
#function
period_mean<-function(x){
tmp<-df2[df$cat == df1[x,]$cat,]
#This line extracts the row name index from tmp,
#in which the two dataframes match on date and hour
he_i<-which(tmp$date == df1[x,]$date & tmp$hour == df1[x,]$hour)
#My lagged period is given by the variable "lag". I want the average
#over the period hour - (hour - lag). Since df2 is sorted such hours
#are consecutive, this method requires that I subset on only the
#relevant value for cat (hence the creation of tmp in the first line
#of the function
p<-mean(tmp[(he_i - df1[x,]$lag):he_i,]$p)
print(x)
print(p)
return(p)
}
#Execute function
out<-sapply(1:length(row.names(df1)), period_mean)
编辑我随后了解到,我最初的问题迭代如此缓慢的部分原因是我的两个数据帧之间的数据类不一样。 df1$date 是一个日期字段,而 df2$date 是一个字符字段。当然,这在我发布的示例中并不明显,因为数据类型在构造上是相同的。希望这可以帮助。
【问题讨论】:
-
它应该如下所示。 tmp
-
你的函数中还有其他几个
dfs,你的意思是df1在所有这些情况下? -
我的意思是 df1。我再次编辑了原始帖子。抱歉打错字了。
标签: r function loops optimization