1

I'm applying ddply to the following data frame. The point is to apply ecdf function to yearly_test_count value to rows that have the same country.

> 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

This is the script I used:

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

But I received the following error:

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

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

I tried using the function ecdf alone with yearly_test_count column and it works:

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

Anyone has any idea why this doesn't work when using ddply?

This is weird since the script worked before, now I run the script again and encounter the mentioned error. I'm not sure if this issue is related to different in versions of R or versions of the package?

Any help is much appreciated ! :)

Tara Sutjarittham
  • 347
  • 1
  • 5
  • 17
  • @akrun ecdf is empirical cumulative distribution function. More details here: https://stat.ethz.ch/R-manual/R-devel/library/stats/html/ecdf.html – Tara Sutjarittham Dec 05 '16 at 05:02
  • a similar question was here `http://stackoverflow.com/questions/6955128/object-not-found-error-with-ddply-inside-a-function`. Would this help? – joel.wilson Dec 05 '16 at 06:44
  • bdw which version of `plyr` do you have? because the code is working for me. mine is haveing version `plyr_1.8.4` – joel.wilson Dec 05 '16 at 06:49

1 Answers1

1

One option would be using ave from base R

test$ecdf_val <-  with(test, ave(yearly_test_count, country, 
                       FUN = function(x) ecdf(x)(x)*length(x)))
akrun
  • 674,427
  • 24
  • 381
  • 486