【问题标题】:R - Object not found error when using ddplyR - 使用 ddply 时找不到对象错误
【发布时间】:2023-03-24 22:54:01
【问题描述】:

我正在将 ddply 应用于以下数据框。重点是将 ecdf 函数应用于 yearly_test_count 值到具有相同国家/地区的行。

> 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 单独与 yearly_test_count 列一起使用,它可以工作:

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

有人知道为什么在使用 ddply 时这不起作用吗?

这很奇怪,因为脚本之前工作过,现在我再次运行脚本并遇到上述错误。我不确定这个问题是否与不同版本的 R 或包的版本有关?

非常感谢任何帮助! :)

【问题讨论】:

  • @akrun ecdf 是经验累积分布函数。更多细节在这里:stat.ethz.ch/R-manual/R-devel/library/stats/html/ecdf.html
  • 这里有一个类似的问题http://stackoverflow.com/questions/6955128/object-not-found-error-with-ddply-inside-a-function。这会有帮助吗?
  • bdw 你有哪个版本的plyr?因为代码对我有用。我的版本是plyr_1.8.4

标签: r object plyr ecdf


【解决方案1】:

一种选择是使用来自base Rave

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

【讨论】:

    猜你喜欢
    • 2011-10-20
    • 2017-06-12
    • 2018-08-15
    • 2017-12-19
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    相关资源
    最近更新 更多