如何用空格引用变量名?

时间:2022-12-25 19:31:40

In ggplot2, how do I refer to a variable name with spaces?

在ggplot2中,如何使用空格引用变量名?

Why do qplot() and ggplot() break when used on variable names with quotes?

为什么qplot()和ggplot()在带引号的变量名上使用时会中断?

For example, this works:

例如,这有效:

qplot(x,y,data=a)

But this does not:

但这不是:

qplot("x","y",data=a)

I ask because I often have data matrices with spaces in the name. Eg, "State Income". ggplot2 needs data frames; ok, I can convert. So I'd want to try something like:

我问,因为我经常在名称中包含带空格的数据矩阵。例如,“国家收入”。 ggplot2需要数据帧;好的,我可以转换。所以我想尝试类似的东西:

qplot("State Income","State Ideology",data=as.data.frame(a.matrix))

That fails.

那失败了。

Whereas in base R graphics, I'd do:

而在基础R图形中,我会这样做:

plot(a.matrix[,"State Income"],a.matrix[,"State Ideology"])

Which would work.

哪个会奏效。

Any ideas?

有任何想法吗?

3 个解决方案

#1


22  

Answer: because 'x' and 'y' are considered a length-one character vector, not a variable name. Here you discover why it is not smart to use variable names with spaces in R. Or any other programming language for that matter.

答案:因为'x'和'y'被认为是长度为一的字符向量,而不是变量名称。在这里你会发现为什么在R中使用带空格的变量名称是不明智的。或者任何其他编程语言。

To refer to variable names with spaces, you can use either hadleys solution

要使用空格引用变量名,可以使用hadleys解决方案

a.matrix <- matrix(rep(1:10,3),ncol=3)
colnames(a.matrix) <- c("a name","another name","a third name")

qplot(`a name`, `another name`,data=as.data.frame(a.matrix)) # backticks!

or the more formal

或者更正式

qplot(get('a name'), get('another name'),data=as.data.frame(a.matrix))

The latter can be used in constructs where you pass the name of a variable as a string in eg a loop construct :

后者可以在构造中使用,在这种构造中,您将变量的名称作为字符串传递给例如循环构造:

for (i in c("another name","a third name")){
    print(qplot(get(i),get("a name"),
      data=as.data.frame(a.matrix),xlab=i,ylab="a name"))
    Sys.sleep(5)
}

Still, the best solution is not to use variable names with spaces.

不过,最好的解决方案是不要使用带空格的变量名。

#2


3  

Using get is not more "formal", actually I would argue the opposite. As the R help says (help("`")), you can almost always use a variable name that contains spaces, provided it's quoted. (Normally, with a backtick, as already suggested.)

使用get不是更“正式”,实际上我认为相反。正如R帮助所说(help(“`”)),如果引用它,你几乎总能使用包含空格的变量名。 (通常情况下,正如已经建议的那样使用反引号。)

#3


3  

Something similar was asked on ggplot2 mailing list and Mehmet Gültaş linked to this post. Another way of using strings to construct your ggplot call is through the aes_strings function. Note that you still have to put backticks inside the quotes for the thing to work for variables with spaces.

在ggplot2邮件列表和MehmetGültaş上发布了与此帖相关的类似内容。使用字符串构建ggplot调用的另一种方法是通过aes_strings函数。请注意,您仍然必须在引号内放置反引号,以便为包含空格的变量工作。

library(ggplot2)

names(mtcars)[1] <- "em pi dzi"

ggplot(mtcars, aes_string(x = "cyl", y = "`em pi dzi`")) +
  theme_bw() +
  geom_jitter()

#1


22  

Answer: because 'x' and 'y' are considered a length-one character vector, not a variable name. Here you discover why it is not smart to use variable names with spaces in R. Or any other programming language for that matter.

答案:因为'x'和'y'被认为是长度为一的字符向量,而不是变量名称。在这里你会发现为什么在R中使用带空格的变量名称是不明智的。或者任何其他编程语言。

To refer to variable names with spaces, you can use either hadleys solution

要使用空格引用变量名,可以使用hadleys解决方案

a.matrix <- matrix(rep(1:10,3),ncol=3)
colnames(a.matrix) <- c("a name","another name","a third name")

qplot(`a name`, `another name`,data=as.data.frame(a.matrix)) # backticks!

or the more formal

或者更正式

qplot(get('a name'), get('another name'),data=as.data.frame(a.matrix))

The latter can be used in constructs where you pass the name of a variable as a string in eg a loop construct :

后者可以在构造中使用,在这种构造中,您将变量的名称作为字符串传递给例如循环构造:

for (i in c("another name","a third name")){
    print(qplot(get(i),get("a name"),
      data=as.data.frame(a.matrix),xlab=i,ylab="a name"))
    Sys.sleep(5)
}

Still, the best solution is not to use variable names with spaces.

不过,最好的解决方案是不要使用带空格的变量名。

#2


3  

Using get is not more "formal", actually I would argue the opposite. As the R help says (help("`")), you can almost always use a variable name that contains spaces, provided it's quoted. (Normally, with a backtick, as already suggested.)

使用get不是更“正式”,实际上我认为相反。正如R帮助所说(help(“`”)),如果引用它,你几乎总能使用包含空格的变量名。 (通常情况下,正如已经建议的那样使用反引号。)

#3


3  

Something similar was asked on ggplot2 mailing list and Mehmet Gültaş linked to this post. Another way of using strings to construct your ggplot call is through the aes_strings function. Note that you still have to put backticks inside the quotes for the thing to work for variables with spaces.

在ggplot2邮件列表和MehmetGültaş上发布了与此帖相关的类似内容。使用字符串构建ggplot调用的另一种方法是通过aes_strings函数。请注意,您仍然必须在引号内放置反引号,以便为包含空格的变量工作。

library(ggplot2)

names(mtcars)[1] <- "em pi dzi"

ggplot(mtcars, aes_string(x = "cyl", y = "`em pi dzi`")) +
  theme_bw() +
  geom_jitter()