Set column names while calling a function

我们两清 提交于 2019-12-04 18:09:45
Blue Magister

It is possible, but it obfuscates your code. The tradeoff between brevity and clarity here is acute.

To understand how it works, I reference this question.

colnames(x) <- y

Is internally rewritten as

x <- `colnames<-`(x,y)

You can then do the translation yourself.

bar <- `colnames<-`(combn(colnames(foo), 2, function(x) foo[,x[1]] + foo[,x[2]]),
                    apply(combn(colnames(foo),2), 2, paste0,collapse=""))

In many cases, however, it's not worth the mental and syntactic gymnastics required to collapse lines of code in this way. Multiple lines tend to be clearer to follow.

You start with a data.frame, not a matrix. Not that important, but it helps to keep up with the jargon we usually use.

What you're after is not possible. If you look at the code of combn, when the result is simplified, it uses no dimension names.

    }
    if (simplify) 
        array(out, dim.use)
    else out
}

You can either hack the function and make it add dimension names, or, you can add it manually to your result post festum.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!