如何返回列的名称而不是它包含的数据?

时间:2015-09-26 21:27:15

标签: r

我试图获取要打印的对象的名称而不是它所代表的值。

这可能吗?例如,如何更改以下as.character()命令以返回文本' foo'和' bar'而不是他们所代表的价值。

custom_plot<-function(X, Y){
plot(X, Y, main =paste("Custom Plot ", as.character(Y), "vs.", as.character(X)))
} 

foo<-c(1,2,3)
bar<-c(2,4,5)

custom_plot(foo, bar) 
#Should make a plot with title of "Custom Plot foo vs. bar"

1 个答案:

答案 0 :(得分:5)

我们可以使用substitute

custom_plot<-function(X, Y){
  plot(X, Y, main =paste("Custom Plot ", substitute(Y), "vs.", substitute(X)))
}