依赖于R中非标准评估的函数的包装器 [英] Wrapper for a function relying on non-standard evaluation in R

查看:72
本文介绍了依赖于R中非标准评估的函数的包装器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ftable周围写了一个包装器,因为我需要计算许多变量的频率和百分比的平面表:

I wrote a wrapper around ftable because I need to compute flat tables with frequency and percentage for many variables:

mytable <- function(...) {
    tab <- ftable(...,
                  exclude = NULL)
    prop <- prop.table(x = tab,
                       margin = 2) * 100
    bind <- cbind(as.matrix(x = tab),
                  as.matrix(x = prop))
    margin <- addmargins(A = bind,
                         margin = 1)
    round(x = margin,
          digits = 1)
}

mytable(formula = wool + tension ~ breaks,
        data = warpbreaks)

    A_L A_M A_H B_L B_M B_H   A_L   A_M   A_H   B_L   B_M   B_H
10    0   0   1   0   0   0   0.0   0.0  11.1   0.0   0.0   0.0
12    0   1   0   0   0   0   0.0  11.1   0.0   0.0   0.0   0.0
13    0   0   0   0   0   1   0.0   0.0   0.0   0.0   0.0  11.1
14    0   0   0   1   0   0   0.0   0.0   0.0  11.1   0.0   0.0
15    0   0   1   0   0   2   0.0   0.0  11.1   0.0   0.0  22.2
...
Sum   9   9   9   9   9   9 100.0 100.0 100.0 100.0 100.0 100.0

但是,我不能将ftable中的subset参数与函数一起使用,也不能与最小的mytable <- function(...) ftable(...)一起使用:

However, I can not use the subset argument from ftable with my function, nor with the minimal mytable <- function(...) ftable(...):

mytable(formula = wool + tension ~ breaks,
        data = warpbreaks,
        subset = breaks < 20)

 Error in eval(substitute(subset), data, env) : 
  ..3 used in an incorrect context, no ... to look in

我知道我可以使用data = warpbreaks[warpbreaks$breaks < 20, ]作为数据参数的子集,但我希望提高对R的了解."

I know I can subset in the data argument with data = warpbreaks[warpbreaks$breaks < 20, ] as a workaround, but I am looking to improve my knowledge of R. "Advanced R" helped me to understand that the error is due to non-standard evaluation, but I did not manage to correct my code.

所以我的问题是:

  • 如何告诉R在warpbreaks中寻找breaks?
  • 更一般地说,是否存在更明显的基本R方法来计算单个变量和多个变量在垂直布局中具有频率和百分比的平面表? (我可以使用mytable(x = warpbreaks$tension, row.vars = 1)获得单个变量的垂直布局.)
  • How can I tell R to look for breaks in warpbreaks ?
  • More generally, is there a more obvious base R way to compute flat tables with frequency and percentage in a vertical layout for both single and multiple variables ? (I can get a vertical layout for a single variable with mytable(x = warpbreaks$tension, row.vars = 1).)

推荐答案

在没有...的函数定义下,我得到了另一个错误:

With a function definition without ..., I get a different error:

mytable <- function(formula,
                    data,
                    subset) ftable(formula = formula,
                                   data = data,
                                   subset = subset)

mytable(formula = wool + tension ~ breaks,
        data = warpbreaks,
        subset = breaks < 20)

 Error in xj[i] : invalid subscript type 'closure'

此错误使我转向以前没有找到的资源.

This error led me to ressources I havent found before.

有些 线程带我去:

# function 1
mytable <- function(...) {
    mc <- match.call()
    mc["exclude"] <- list(NULL)
    do.call(what = ftable,
            args = as.list(x = mc[-1]))
    #etc
}

write.csv系列和lm源代码引导我:

The write.csv family and lm source code led me to:

# function 2
mytable <- function(...) {
    mc <- match.call()
    mc[[1]] <- quote(expr = ftable)
    mc["exclude"] <- list(NULL)
    eval(expr = mc)
    # etc
}

但是,我正在寻找两种方法(功能1和功能2)的优缺点,因为我不知道某个方法是否会受到青睐.到目前为止,我只是发现do.call可能会慢一些.

However, I am looking for pro and cons of both methods (function 1 and function 2), because I do not know if a method is to be favored. So far I just found that do.call might be slower.

更重要的是,这些方法导致了另一个问题:我不能再将包装器与lapplywith一起使用.

More importantly, these methods led my to another issue: I can not use my wrapper with lapply and with anymore.

这篇关于依赖于R中非标准评估的函数的包装器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆