我在 R 中遇到了很大的性能问题。我编写了一个迭代 data.frame
对象的函数。它只是向 data.frame
添加一个新列并积累一些内容。 (操作简单)。 data.frame
大约有 850K 行。我的电脑仍在工作(现在大约 10 小时),我不知道运行时间。
dayloop2 <- function(temp){
for (i in 1:nrow(temp)){
temp[i,10] <- i
if (i > 1) {
if ((temp[i,6] == temp[i-1,6]) & (temp[i,3] == temp[i-1,3])) {
temp[i,10] <- temp[i,9] + temp[i-1,10]
} else {
temp[i,10] <- temp[i,9]
}
} else {
temp[i,10] <- temp[i,9]
}
}
names(temp)[names(temp) == "V10"] <- "Kumm."
return(temp)
}
有什么想法可以加快此操作吗?
最佳答案
最大的问题和无效的根源是索引 data.frame,我的意思是使用 temp[,]
的所有这些行。
尽量避免这种情况。我接受了你的函数,更改了索引,这里version_A
dayloop2_A <- function(temp){
res <- numeric(nrow(temp))
for (i in 1:nrow(temp)){
res[i] <- i
if (i > 1) {
if ((temp[i,6] == temp[i-1,6]) & (temp[i,3] == temp[i-1,3])) {
res[i] <- temp[i,9] + res[i-1]
} else {
res[i] <- temp[i,9]
}
} else {
res[i] <- temp[i,9]
}
}
temp$`Kumm.` <- res
return(temp)
}
如您所见,我创建了收集结果的向量res
。最后,我将其添加到 data.frame
中,并且不需要弄乱名称。
那么如何更好呢?
我使用 nrow
从 1,000 到 10,000 × 1,000 运行 data.frame
的每个函数,并使用 system.time
测量时间
X <- as.data.frame(matrix(sample(1:10, n*9, TRUE), n, 9))
system.time(dayloop2(X))
结果是
您可以看到您的版本与nrow(X)
呈指数关系。修改后的版本具有线性关系,简单的lm
模型预测850,000行的计算需要6分10秒。
矢量化的力量
正如 Shane 和 Calimo 在他们的回答中指出的那样,矢量化是提高性能的关键。 从您的代码中,您可以移出循环:
- 调理
- 结果初始化(
temp[i,9]
)
这导致了这段代码
dayloop2_B <- function(temp){
cond <- c(FALSE, (temp[-nrow(temp),6] == temp[-1,6]) & (temp[-nrow(temp),3] == temp[-1,3]))
res <- temp[,9]
for (i in 1:nrow(temp)) {
if (cond[i]) res[i] <- temp[i,9] + res[i-1]
}
temp$`Kumm.` <- res
return(temp)
}
比较此函数的结果,这次是 nrow
从 10,000 到 100,000,乘以 10,000。
调整已调音
另一个调整是将循环索引 temp[i,9]
更改为 res[i]
(这在第 i 次循环迭代中完全相同) 。
这又是索引向量和索引 data.frame
之间的区别。
第二件事:当您查看循环时,您可以看到不需要循环所有 i
,而只需循环那些符合条件的。
那么我们开始吧
dayloop2_D <- function(temp){
cond <- c(FALSE, (temp[-nrow(temp),6] == temp[-1,6]) & (temp[-nrow(temp),3] == temp[-1,3]))
res <- temp[,9]
for (i in (1:nrow(temp))[cond]) {
res[i] <- res[i] + res[i-1]
}
temp$`Kumm.` <- res
return(temp)
}
您获得的性能很大程度上取决于数据结构。精确 - 条件中 TRUE
值的百分比。
对于我的模拟数据,850,000 行的计算时间低于一秒。
我希望你能走得更远,我认为至少有两件事可以做:
- 编写
C
代码来执行条件 cumsum 如果您知道数据中的最大序列并不大,那么您可以将循环更改为矢量化 while,例如
while (any(cond)) { indx <- c(FALSE, cond[-1] & !cond[-n]) res[indx] <- res[indx] + res[which(indx)-1] cond[indx] <- FALSE }
用于模拟和图形的代码是 available on GitHub .
关于performance - 加速R中的循环操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2908822/