如何调用其他函数内部的函数

时间:2015-04-02 09:29:27

标签: r function

我正在尝试从先前估计的位置绘制的速度矢量中均衡加速度。问题是我不知道它是否适用于另一个函数中的函数,如下所示:

SpeedReckon <- function(trip)
{
  speed=3.6*sqrt(diff(trip$x,1,1)^2+diff(trip$y,1,1)^2) 
  return(speed)
}

Acceleration <- function(speed){
  acceleration = cbind(positive,negative) #separation between positive          acceleration and negative  I want to create a variable with two features (positive and negative -> like a list 
  acc = diff(speed) # vector with accelerations
  i=1
  while (i<=length(acc)) #classify positive and negative
  {
    j=1
    if(acc[i]<0)
    {

      while(positive[j]!=NULL){ # I do this in order to add the values in the last space, I don't know if it is necessary
        j=j+1
      }
      positive[j] <- acc[i]
    }
    else{

      while(negative[j]!=NULL){
        j=j+1
      }
      negative[j] <- acc[i]
    }
    i=i+1
  }
  return (acceleration)
}

上面的代码找不到acceleration函数,所以我想我没有正确调用function。这是function电话:

trip = read.csv(paste0(dirPath,i,".csv"))
speed = c(SpeedReckon(trip))

feature2 = c(driver,i, Acceleration(speed))
acceleration = rbind(acceleration, feature2)

1 个答案:

答案 0 :(得分:0)

我不明白为什么你认为Acceleration在另一个函数里面,它不是。

运行代码会产生以下错误:

Acceleration(2)
Error in cbind(positive, negative) : object 'positive' not found

这是真的,你没有在任何地方定义positivenegative,除非它们在你的代码中的其他地方定义,并且应用的范围规则成功产生结果。

请更改此功能的参数列表以包含positivenegative,或者如果您要将函数外的值提供为global variables

相关问题