函数在循环中使用时会产生不同的结果

时间:2016-03-31 09:15:05

标签: r function loops

我想在我的数据上使用这个公式,但它给了我不同的结果。我不确定它是来自循环还是功能。 有什么建议? 谢谢,

原始公式和脚本:

   vol <-c(542,123,111) 
    d2elec<- c(0,1,2)
    df<- as.data.frame(cbind(vol,d2elec))

    Vp_lambda <- function(L, df) {vsum <- sum(df[df$d2elec<=L & df$d2elec>0,'vol'])
    return (1/abs(L) * vsum)
    }

结果:

Vp_lambda(2,df)
[1] 117

这是我的公式和脚本:

vp_lambda <- function (date,dataframe) {vsum <- sum(dataframe[dataframe$days<=date & dataframe$days > 0,"length"])
return (1/abs(date)*vsum)}

结果:

 vp_lambda(1,test)
[1] 4901

当我将此函数运行到循环中时,它会给出不同的结果:

for (i in test){
  test$absolute<-vp_lambda(test$days,test)
}

结果:

test[1,4]
Source: local data frame [1 x 1]

  absolute
     (dbl)
1   278845

我的数据:

structure(list(date = structure(1:31, .Label = c("2015-05-08", 
"2015-05-09", "2015-05-10", "2015-05-11", "2015-05-12", "2015-05-13", 
"2015-05-14", "2015-05-15", "2015-05-16", "2015-05-17", "2015-05-18", 
"2015-05-19", "2015-05-20", "2015-05-21", "2015-05-22", "2015-05-23", 
"2015-05-24", "2015-05-25", "2015-05-26", "2015-05-27", "2015-05-28", 
"2015-05-29", "2015-05-30", "2015-05-31", "2015-06-01", "2015-06-02", 
"2015-06-03", "2015-06-04", "2015-06-05", "2015-06-06", "2015-06-07"
), class = "factor"), length = c(4901L, 5889L, 5314L, 3328L, 
2688L, 1447L, 2003L, 1300L, 1671L, 1415L, 4545L, 7415L, 6319L, 
7622L, 6300L, 8464L, 12466L, 6538L, 7737L, 8244L, 9089L, 6276L, 
9804L, 8551L, 5763L, 6713L, 10850L, 14673L, 28028L, 19956L, 53536L
), days = 1:31, absolute = c(278845, 139422.5, 92948.3333333333, 
69711.25, 55769, 46474.1666666667, 39835, 34855.625, 30982.7777777778, 
27884.5, 25349.5454545455, 23237.0833333333, 21449.6153846154, 
19917.5, 18589.6666666667, 17427.8125, 16402.6470588235, 15491.3888888889, 
14676.0526315789, 13942.25, 13278.3333333333, 12674.7727272727, 
12123.6956521739, 11618.5416666667, 11153.8, 10724.8076923077, 
10327.5925925926, 9958.75, 9615.34482758621, 9294.83333333333, 
8995)), .Names = c("date", "length", "days", "absolute"), row.names = c(NA, 
-31L), class = c("tbl_df", "tbl", "data.frame"))

1 个答案:

答案 0 :(得分:2)

你的意思是:

for (i in 1:nrow(test)) test$absolute[i]<-vp_lambda(test$days[i],test)

目前,您提供的test$days是一个向量,为date。这会使dataframe$days<=date混乱,因为它评估为:

test$days <= test$days
[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[13] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[25] TRUE TRUE TRUE TRUE TRUE TRUE TRUE

这就是vsum变为278845的原因,它等于sum(test$length)。如果我们将其插入1/abs(date)*vsum,我们会返回长度为31的向量,因为date1:31的向量。

有多种方法可以解决这个问题(例如矢量化),但我的猜测是你尝试了我在开头提供的解决方案,所以这就是我给出的解决方案。使用for循环时,请确保您知道i是什么(在这种情况下是1到31的序列),并确保在{{1}内的代码中实现i 1}}循环(在这种情况下通过for索引)。

相关问题