【发布时间】:2014-11-18 14:38:14
【问题描述】:
我在下面创建了以下函数,用于在 data.frame 的新列中将真实值与预测值(当真实值不存在时)合并,该函数确实有效,但我想对其进行优化,因为使用数据集我工作,该功能大约需要两个小时才能运行..如果有人可以帮助我,我将不胜感激。
p <-
function(object, newdata = NULL, type = c("link", "response", "terms"),
rse.fit = FALSE, dispersion = NULL, terms = NULL,
na.action = na.pass, ...)
{
{
pred <- predict (object,newdata)
}
vetor1 <- (newdata$ALT) # Creates a column vector from the actual heights of the data.frame
vetor1[is.na(vetor1)] <- 0 # Replaces the NA's present in the vector created above the numeric value 0
vetor2 <- c(pred) # Creates a vector from the predicted data
for(i in 1:length(vetor1)){ # The loop is executed until all values vector1 pass the following condition
if(vetor1[i]==0.00){ # If a value of the first vector has the value 0, ie, if it is absent
vetor1[i]=vetor2[i] # Then the predicted value will replace the missing value
newdata$ALTMISTA <- vetor1 # The vector1, already possessing the actual values and the predicted values merged into the same vector goes on to become a new column in data.frame, this column is called a ALTMISTA
}
}
return (newdata)
}
【问题讨论】:
-
您好,欢迎来到 StackOverflow!有关优化代码的问题应在CodeReview StackExchange 提出
-
@JohnOdom 不一定。我们经常在这里考虑(写得很好:-))对代码模块的请求,因为经常有一些现有的库或 R 包可以大大加快各种数据处理步骤。
-
@CarlWitthoft 哦,好吧,我想我误解了两页之间的区别,谢谢!
标签: r function loops for-loop optimization