如何更改函数中的向量名称? [复制]

Posted

技术标签:

【中文标题】如何更改函数中的向量名称? [复制]【英文标题】:How to change a vector name in a function? [duplicate] 【发布时间】:2022-01-03 02:50:02 【问题描述】:

我想使用函数进行图形输出,例如箱线图或图表。这样我就可以绘制多个数据框,每次只更改列名。

例如:

boxplot_func = function(column)
  boxplot(dataframe1$column, dataframe2$column)

boxplot_func(mean)
boxplot_func(max)
etc.

但 R 似乎没有在函数中计算 meanmax。你知道怎么做吗?

【问题讨论】:

【参考方案1】:

一种选择是将列作为字符串传递并使用[[ 访问函数中的列:

一个使用mtcars的简单例子:

boxplot_func = function(column) 
  boxplot(mtcars[[column]], mtcars[[column]])


boxplot_func("mpg")

【讨论】:

以上是关于如何更改函数中的向量名称? [复制]的主要内容,如果未能解决你的问题,请参考以下文章