使用带字符串 col 名称的 data.frames 调用 qplot

发布于 2024-11-17 00:44:27 字数 206 浏览 0 评论 0原文

我创建了一个 data.frame:

d <- data.frame("name 1"=1, "name 2"=2)

由于显而易见的原因,调用 qplot("name 1", "name 2", data=d) 不起作用。是否可以使用某种机制使其工作?我已经尝试过 as.name 但这也不起作用。

I have a data.frame created with:

d <- data.frame("name 1"=1, "name 2"=2)

Calling qplot("name 1", "name 2", data=d) does not work, for obvious reasons. Is it possible to make it work using some mechanism? I've tried as.name but that also doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百变从容 2024-11-24 00:44:27

这里有两个问题:

  1. 如何使用非标准但语法上有效的组件名称,以及
  2. 在为数据框创建名称组件时 R 正在做什么。

我会以相反的顺序来处理这些。

当您使用data.frame()创建数据框时,默认情况下会检查组件名称的有效性。从 ?data.frame 我们得到:

check.names: logical.  If ‘TRUE’ then the names of the variables in the
          data frame are checked to ensure that they are syntactically
          valid variable names and are not duplicated.  If necessary
          they are adjusted (by ‘make.names’) so that they are.

这意味着对于 OP d,使用创建的

d <- data.frame("name 1" = 1, "name 2" = 2)

将具有这些名称:

> names(d)
[1] "name.1" "name.2"

来抑制

> names(data.frame("name 1" = 1, "name 2" = 2, check.names = FALSE))
[1] "name 1" "name 2"

这种行为可以通过默认行为方式 我们可以做类似

> d$name.1
[1] 1

或 之

> qplot(name.1, name.2, data=d)

类的事情,但是如果您想要输入输入时的名称并带有空格怎么办?显然它们不再像以前那样工作:

> d <- data.frame("name 1" = 1, "name 2" = 2, check.names = FALSE)
> d
  name 1 name 2
1      1      2
> d$name 1
Error: unexpected numeric constant in "d$name 1"

非标准的、语法上有效的名称在使用时需要加引号,使用反引号 `foo` 或普通引号 "foo"'foo',例如

> d

这里有两个问题:

  1. 如何使用非标准但语法上有效的组件名称,以及
  2. 在为数据框创建名称组件时 R 正在做什么。

我会以相反的顺序来处理这些。

当您使用data.frame()创建数据框时,默认情况下会检查组件名称的有效性。从 ?data.frame 我们得到:

check.names: logical.  If ‘TRUE’ then the names of the variables in the
          data frame are checked to ensure that they are syntactically
          valid variable names and are not duplicated.  If necessary
          they are adjusted (by ‘make.names’) so that they are.

这意味着对于 OP d,使用创建的

d <- data.frame("name 1" = 1, "name 2" = 2)

将具有这些名称:

> names(d)
[1] "name.1" "name.2"

来抑制

> names(data.frame("name 1" = 1, "name 2" = 2, check.names = FALSE))
[1] "name 1" "name 2"

这种行为可以通过默认行为方式 我们可以做类似

> d$name.1
[1] 1

或 之

> qplot(name.1, name.2, data=d)

类的事情,但是如果您想要输入输入时的名称并带有空格怎么办?显然它们不再像以前那样工作:

> d <- data.frame("name 1" = 1, "name 2" = 2, check.names = FALSE)
> d
  name 1 name 2
1      1      2
> d$name 1
Error: unexpected numeric constant in "d$name 1"

非标准的、语法上有效的名称在使用时需要加引号,使用反引号 `foo` 或普通引号 "foo"'foo',例如

name 1` [1] 1 > d$"name 1" [1] 1 > d

,在 qplot() 示例中,带引号的组件名称无法按预期工作。该图是在字符值 “name 1”“name 2” 处绘制的,而不是这些变量 1 的 2

name 1'

,在 qplot() 示例中,带引号的组件名称无法按预期工作。该图是在字符值 “name 1”“name 2” 处绘制的,而不是这些变量 1 的2

There are two issues at work here:

  1. How to use non-standard yet syntactically valid component names, and
  2. What R is doing when creating the names component for a data frame.

I'll take these in reverse order.

When you create a data frame using data.frame(), the default is the check the names of the components for validity. From ?data.frame we have:

check.names: logical.  If ‘TRUE’ then the names of the variables in the
          data frame are checked to ensure that they are syntactically
          valid variable names and are not duplicated.  If necessary
          they are adjusted (by ‘make.names’) so that they are.

What this means is that for the the OPs d, created using

d <- data.frame("name 1" = 1, "name 2" = 2)

will have these names:

> names(d)
[1] "name.1" "name.2"

This behaviour can be suppressed via

> names(data.frame("name 1" = 1, "name 2" = 2, check.names = FALSE))
[1] "name 1" "name 2"

The default behaviour means that we can do things like

> d$name.1
[1] 1

or

> qplot(name.1, name.2, data=d)

but what if you want to have the names as you entered them, with the space? Clearly they don't work as before:

> d <- data.frame("name 1" = 1, "name 2" = 2, check.names = FALSE)
> d
  name 1 name 2
1      1      2
> d$name 1
Error: unexpected numeric constant in "d$name 1"

Non-standard, syntactically valid names need to be quoted when used, using backticks `foo`, or normal quotes "foo", 'foo', e.g.

> d

There are two issues at work here:

  1. How to use non-standard yet syntactically valid component names, and
  2. What R is doing when creating the names component for a data frame.

I'll take these in reverse order.

When you create a data frame using data.frame(), the default is the check the names of the components for validity. From ?data.frame we have:

check.names: logical.  If ‘TRUE’ then the names of the variables in the
          data frame are checked to ensure that they are syntactically
          valid variable names and are not duplicated.  If necessary
          they are adjusted (by ‘make.names’) so that they are.

What this means is that for the the OPs d, created using

d <- data.frame("name 1" = 1, "name 2" = 2)

will have these names:

> names(d)
[1] "name.1" "name.2"

This behaviour can be suppressed via

> names(data.frame("name 1" = 1, "name 2" = 2, check.names = FALSE))
[1] "name 1" "name 2"

The default behaviour means that we can do things like

> d$name.1
[1] 1

or

> qplot(name.1, name.2, data=d)

but what if you want to have the names as you entered them, with the space? Clearly they don't work as before:

> d <- data.frame("name 1" = 1, "name 2" = 2, check.names = FALSE)
> d
  name 1 name 2
1      1      2
> d$name 1
Error: unexpected numeric constant in "d$name 1"

Non-standard, syntactically valid names need to be quoted when used, using backticks `foo`, or normal quotes "foo", 'foo', e.g.

name 1` [1] 1 > d$"name 1" [1] 1 > d

In the qplot() example, however, the quoted component names doesn't work as expected. The plot is drawn at the character values "name 1" and "name 2" and not the values of those variables, 1 and 2.

name 1'

In the qplot() example, however, the quoted component names doesn't work as expected. The plot is drawn at the character values "name 1" and "name 2" and not the values of those variables, 1 and 2.

舞袖。长 2024-11-24 00:44:27

您可以使用反引号将字符串转换为符号:

qplot(`name 1`, `name 2`, data=d)

很高兴听到这有效;)

You can use backticks to turn strings into symbols:

qplot(`name 1`, `name 2`, data=d)

Glad to hear this worked ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文