2016-12-05 59 views
1

我正在將ddply應用於以下數據框。重點是將ecdf函數應用於具有相同國家/地區的行的annual_test_count值。R - 使用ddply時找不到對象錯誤

> head(test) 
country yearly_test_count download_speed 
1  AU     1  2.736704 
2  AU     6  3.249486 
3  AU     6  2.287267 
4  AU     6  2.677241 
5  AU     6  1.138213 
6  AU     6  3.205364 

這是我使用的腳本:

house_total_year_ecdf <- ddply(test, c("country"), mutate, 
     ecdf_val = ecdf(yearly_test_count)(yearly_test_count)*length(yearly_test_count)) 

但我收到以下錯誤:

Error in eval(substitute(expr), envir, enclos) : 
    object 'yearly_test_count' not found 

=============== ================================================== =

我嘗試使用函數ecdf單獨與annual_test_count列,它的工作原理:

ecdf(test$yearly_test_count)(test$yearly_test_count)*length(test$yearly_test_count) 

任何人有任何想法,爲什麼這在使用ddply時不起作用?

這是奇怪的,因爲腳本工作之前,現在我再次運行腳本,遇到提到的錯誤。我不確定這個問題是否與R版本或軟件包版本中的不同有關?

任何幫助非常感謝! :)

+0

@akrun ECDF是經驗累積分佈函數。更多細節在這裏:https://stat.ethz.ch/R-manual/R-devel/library/stats/html/ecdf.html –

+0

類似的問題在這裏'http://stackoverflow.com/questions/6955128/對象未找到誤差與 - ddply-內部-A-function'。這會有幫助嗎? –

+0

bdw你有哪個版本的'plyr'?因爲代碼正在爲我工​​作。我的版本是'plyr_1.8.4' –

回答

1

一種選擇是使用avebase R

test$ecdf_val <- with(test, ave(yearly_test_count, country, 
         FUN = function(x) ecdf(x)(x)*length(x)))