【问题标题】:Custom Function: Object not found R自定义功能:找不到对象 R
【发布时间】:2017-01-18 16:05:56
【问题描述】:

我有一个名为 dat 的 data.frame。

    colnames(dat)
    [1] "variable"  "weight" 

当我运行 aggregate(weight ~ variable, dat, sum) 时,函数运行时没有错误并返回我期望的值。

但是,当我将aggregate() 嵌入到自定义函数中时,如下所示:

    bins <- function(df, var, wt, n) {
                tmp <- aggregate(wt ~ var, df, sum)

                ####################
                other code not shown
                ####################

                return(tmp)
            }

然后运行out &lt;- bins(df=dat, var=variable, wt=weight, n=5),我收到以下错误消息:

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

我也尝试使用with(),但没有成功。

【问题讨论】:

标签: r


【解决方案1】:

可能不是您正在寻找的确切内容,但我发现尽可能使用字符串更容易:

dat <- data.frame(
  variable = sample(letters[1:5], 100, replace = TRUE),
  variable2 = sample(letters[1:5], 100, replace = TRUE),
  weight = rnorm(100)
)

bins <- function(df, var, wt, n) {
  tmp <- aggregate(
    as.formula(
      paste(
        wt,
        paste(var, collapse = '+'),
        sep = '~')),
    df, sum)
  return(tmp)
}

bins(df = dat, var = 'variable', wt = 'weight', n = 5)

bins(df = dat, var = c('variable', 'variable2'), wt = 'weight', n = 5)

结果:

  variable    weight
1        a  3.962502
2        b -0.137942
3        c -2.435460
4        d  1.557121
5        e -0.471481

   variable variable2      weight
1         a         a  0.15849141
2         b         a  2.31792997
3         c         a -2.67871600
4         d         a  1.29191822
5         e         a  0.93714161
6         a         b  0.58574200
7         b         b  1.78097554
8         c         b  0.41522095
9         d         b  0.32981119
10        e         b -0.95515100
11        a         c  1.66244525
12        b         c -1.92009677
13        c         c -2.53845106
14        d         c -1.03501447
15        e         c -0.53367121
16        a         d  0.27701130
17        b         d -0.54682389
18        c         d  3.28828483
19        d         d  1.58885843
20        e         d  0.02646149
21        a         e  1.27881159
22        b         e -1.76992683
23        c         e -0.92179907
24        d         e -0.61845273
25        e         e  0.05373811

【讨论】:

  • 这解决了我的问题,尽管我仍然不明白为什么我最初做的事情不起作用。
  • (我可以搞砸):这个问题与函数的范围有关,即aggregate() 知道weight ~ variable 部分是一个公式,并在全局环境中调用。正在寻找 dat 对象中的指定列,但在函数中调用正在寻找 wtvar。将str(wt ~ var) 添加为bins() 的第一行,您将看到错误发生的原因。
【解决方案2】:

您可以将简单的列名替换为df[,column],并将列名作为字符串传递:

bins <- function(df, var, wt, n) {
            tmp <- aggregate(df[,wt] ~ df[,var], df, sum)

            ####################
            other code not shown
            ####################

            return(tmp)
        }

使用cars 数据集的示例:

bins <- function(df, var, wt, n) {

  tmp <- aggregate(df[,wt] ~ df[,var], df, sum)


  return(tmp)
}

bins(cars, 'speed', 'dist')

This post 也可能对您有所帮助。

【讨论】:

  • 我想引用提供给函数的参数。当我尝试tmp &lt;- aggregate(df$wt ~ df$var, data=df, FUN=sum) 时,出现以下错误。 model.frame.default 中的错误(公式 = df$wt ~ df$var,data = df):变量“df$wt”的类型无效(NULL)
  • @drumminactuary 好的。我编辑了帖子以遵循您的要求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-18
  • 1970-01-01
  • 2014-09-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多