从"..."给出参数R中右函数的参数 [英] Giving arguments from "..." argument to right function in R

查看:93
本文介绍了从"..."给出参数R中右函数的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数可以计算分类变量和连续变量的矩阵的相关性:

I have a function to compute the correlation of matrix of both categorical and continuous variables:

correlation <- function(matrix, ...) {
    xx <- do.call(rbind, lapply(colnames(mtrc), function(ex_i) {
        ty_i <- wtype(matrix, ex_i)
        yy <- sapply(colnames(mtrc), function(ex_j) {
            ty_j <- wtype(matrix, ex_j)

            if(ty_i == "numeric" & ty_j == "numeric") {
                cor(mtrc[ , c(ex_i, ex_j)], ...)[1, 2]
            } else if(ty_i == "factor" & ty_j == "factor") {
                cramersV(table(mtrc[ , c(ex_i, ex_j)]), ...)
            } else {
                fm <- paste(ex_i, "~", ex_j)
                if(ty_i == "factor") {
                    fm <- paste(ex_j, "~", ex_i)
                }
                fm <- lm(fm, data = mtrc[ , c(ex_i, ex_j)], ...)
                lm.beta(fm)
            }
        })
        names(yy) <- colnames(mtrc)
        yy
    }))
    rownames(xx) <- colnames(mtrc)
    xx
}

我的问题是如何正确地将参数...传递给corcramerVlm.如果用户为cor提供参数并且矩阵中存在类别变量,则这三个函数的参数名称不匹配,因此cramerVlm会引发错误(未使用的参数. ).

My question is how to pass, properly, the argument ... to cor, cramerV and lm. Since the argument's names of these three functions do not match if the user gives an argument for cor and there is a categorical variable in the matrix, the cramerV or lm raises an error (unused argument...).

所以...我欢迎您提出任何解决方案或想法.

So... I'm open to any solution or idea you can have.

推荐答案

我没有意识到Richard Scriven在2014年提出了一个很好的问题:.是的,这是一个重复的问题.但是我将在这里保留我的答案,因为它代表了我的想法(以及我的想法).

I did not realize that there was an excellent question by Richard Scriven at 2014: Split up `...` arguments and distribute to multiple functions, when I made my answer below. So yes, this is a duplicated question. But I will keep my answer here, as it represents what I thought (and what I think).

原始答案

我认为,通过为您的correlation函数提供更精细的控制,效果会更好:

I think this is better, by giving your correlation function a finer control:

correlation <- function(matrix, cor.opt = list(), cramersV.opt = list(), lm.opt = list()) {
    xx <- do.call(rbind, lapply(colnames(mtrc), function(ex_i) {
        ty_i <- wtype(matrix, ex_i)
        yy <- sapply(colnames(mtrc), function(ex_j) {
            ty_j <- wtype(matrix, ex_j)

            if(ty_i == "numeric" & ty_j == "numeric") {
                do.call("cor", c(list(x = mtrc[ , c(ex_i, ex_j)]), cor.opt))[1, 2]
            } else if(ty_i == "factor" & ty_j == "factor") {
                do.call("cramersV", c(list(x = table(mtrc[ , c(ex_i, ex_j)])), cramersV.opt))
            } else {
                fm <- paste(ex_i, "~", ex_j)
                if(ty_i == "factor") {
                    fm <- paste(ex_j, "~", ex_i)
                }
                fm <- do.call("lm", c(list(formula = fm, data = mtrc[ , c(ex_i, ex_j)]), lm.opt))
                lm.beta(fm)
            }
        })
        names(yy) <- colnames(mtrc)
        yy
    }))
    rownames(xx) <- colnames(mtrc)
    xx
}

您可以通过参数cor.optcramersV.optlm.opt传递用于不同功能的不同参数.然后,在函数correlation中,使用do.call()进行所有相关的函数调用.

You can pass different arguments intended for different functions via arguments cor.opt, cramersV.opt and lm.opt. Then, inside your function correlation, use do.call() for all relevant function call.

评论

我喜欢@Roland的想法.他选择使用...,同时根据不同功能的形式参数拆分list(...).另一方面,我要求您手动将这些参数指定到不同的列表中.最后,我们俩都要求您使用do.call()进行函数调用.

I like @Roland's idea. He chooses to use ..., while splitting list(...) according to formal arguments of different functions. On the other hand, I have asked you to manually specify those arguments into different lists. In the end, both of us ask you to use do.call() for function call.

罗兰(Roland)的想法广泛适用,因为它更容易扩展到需要...的更多功能.

Roland's idea is broadly applicable, as it is easier to extend to more functions requiring ....

这篇关于从"..."给出参数R中右函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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