将计算表达式作为命名arg传递给函数(R)中的函数(Passing an evaluated expression as named arg to function within function (

系统教程 行业动态 更新时间:2024-06-14 17:01:34
将计算表达式作为命名arg传递给函数(R)中的函数(Passing an evaluated expression as named arg to function within function (R))

我想扩展一个data.frame以包含一个新列,并为该列提供一个在函数内传递的动态分配的名称。 这是一个简化的例子:

passMyName <-function(df, newColTitle) { df2 <-data.frame(df, newColTitle = rep(NA, nrow(df))) return(df2) } randomDF <-data.frame(a=1:3, b=4:6, c=7:9) passMyName(randomDF, myCustomColTitle)

这将向data.frame添加一个新列,其变量名由函数“newColTitle”定义。 相反,我想将newColTitle评估为“myCustomColTitle”并将其作为此新列的命名标记传递,以便输出data.fram包含一个名为“myCustomColTitle”的新列。

这可能吗? 也许通过substitute() , deparse() , quote() , eval() , paste()等你可以假设我已经阅读过以下关于这类主题的引子(实际上是几次):

http://adv-r.had.co.nz/Computing-on-the-language.html

但是有些细节并没有完全停留在我身上。

谢谢

I would like to expand a data.frame to include a new column & give this column a dynamically assigned name passed within a function. Here is a simplified example:

passMyName <-function(df, newColTitle) { df2 <-data.frame(df, newColTitle = rep(NA, nrow(df))) return(df2) } randomDF <-data.frame(a=1:3, b=4:6, c=7:9) passMyName(randomDF, myCustomColTitle)

This will add a new column to the data.frame with the variable name defined by the function, "newColTitle". Instead, I would like to evaluate newColTitle as "myCustomColTitle" and pass this as the named tag for this new column, such that the output data.fram contains a new column called "myCustomColTitle".

Is this possible? Perhaps via substitute(), deparse(), quote(), eval(), paste() etc. You can assume that I've read the following primer on this type of topic (several times actually):

http://adv-r.had.co.nz/Computing-on-the-language.html

But some details have not quite stuck with me.

Thanks

最满意答案

我认为如果你将新名称作为字符值传递,这将更有意义。 例如

passMyName <-function(df, newColTitle) { df2 <- cbind(df, setNames(list(rep(NA, nrow(df))), newColTitle)) return(df2) } randomDF <-data.frame(a=1:3, b=4:6, c=7:9) passMyName(randomDF , "myCustomColTitle")

但如果您真的想使用未评估的表达式,则可以使用

passMyName <-function(df, newColTitle) { newColTitle <- deparse(substitute(newColTitle)) df2 <- cbind(df, setNames(list(rep(NA, nrow(df))), newColTitle)) return(df2) } randomDF <-data.frame(a=1:3, b=4:6, c=7:9) passMyName(randomDF, myCustomColTitle)

I think this would make much more sense if you passed the new name in as a character value. For example

passMyName <-function(df, newColTitle) { df2 <- cbind(df, setNames(list(rep(NA, nrow(df))), newColTitle)) return(df2) } randomDF <-data.frame(a=1:3, b=4:6, c=7:9) passMyName(randomDF , "myCustomColTitle")

but if you really wanted to use an unevaluated expression, you can use

passMyName <-function(df, newColTitle) { newColTitle <- deparse(substitute(newColTitle)) df2 <- cbind(df, setNames(list(rep(NA, nrow(df))), newColTitle)) return(df2) } randomDF <-data.frame(a=1:3, b=4:6, c=7:9) passMyName(randomDF, myCustomColTitle)

更多推荐

本文发布于:2023-04-20 19:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/6d96e3c9c7ffede4bafb6ce056612ab2.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   表达式   arg   Passing   function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!