2015-04-02 47 views
0

我試圖從以前估計的位置繪製的速度矢量中均衡加速度。問題是,我不知道這是否與函數中的另一種函數內部爲以下形式:如何調用其他函數內部的函數

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

根據您發佈的代碼,'加速'不*在另一個功能內。 – 2015-04-02 09:32:35

回答

0

我不明白爲什麼你認爲Acceleration是另一個函數裏面,它不是。

運行你的代碼產生以下錯誤:

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

這是真的,你還沒有定義positivenegative任何地方,除非他們確定別的地方在你的代碼和應用的範圍規則繼承產生一個結果。

請,要麼改變這個函數的參數列表,包括positivenegative或請對您的問題發表評論,如果您是在功能之外提供值global variables