我可以转义变量名中的字符吗? [英] Can I escape characters in variable names?

查看:65
本文介绍了我可以转义变量名中的字符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候,给变量命名是很有用的,就像没有程序员应该为他或她的变量命名一样。当然,在愚蠢的变量名称上有约定和限制的一些充分理由,但我还是很好。特别是在像R这样的语言中,该语言经常用于创建图形以及带有图形的一些标签。因此,某些标签包含变量名。

Sometimes it would be useful to name variables like no programmer should name his or her variables. Of course there's some good reason for conventions and limitations on stoopid variable names, but still I'd be nice. Particularly in a language like R that is used frequently to create graphs and some labels with the graphs. Thus, some labels contain variable names.

有没有办法在R中使用像a + b这样的变量名?还是有类似显示名称的名称?例如,在使用ggplot2进行刻面处理时,这样的选择会很棒。

Is there a way to use something like a+b as a variable name in R? Or is there something like a display name? For example in faceting with ggplot2 such an option would be great.

p_big + facet_grid(x ~ y,scales="free") +labs(x="",y="")

# with x containing a+b, d&c 

提前提出任何想法!

推荐答案

您可以使用反引号:

R> `a + b` <- 3
R> `a + b`
[1] 3

tmp <- data.frame(1:10, rnorm(10))
names(tmp) <- c("a+b", "c&d")
ggplot(tmp, aes(`a+b`, `c&d`)) + geom_point()

另请参见行情

这篇关于我可以转义变量名中的字符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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