R - 使用ddply时找不到对象错误
问题描述:
我正在将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版本或软件包版本中的不同有关?
任何帮助非常感谢! :)
答
一种选择是使用ave
从base R
test$ecdf_val <- with(test, ave(yearly_test_count, country,
FUN = function(x) ecdf(x)(x)*length(x)))
@akrun ECDF是经验累积分布函数。更多细节在这里:https://stat.ethz.ch/R-manual/R-devel/library/stats/html/ecdf.html –
类似的问题在这里'http://*.com/questions/6955128/对象未找到误差与 - ddply-内部-A-function'。这会有帮助吗? –
bdw你有哪个版本的'plyr'?因为代码正在为我工作。我的版本是'plyr_1.8.4' –