使用带字符串 col 名称的 data.frames 调用 qplot
我创建了一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有两个问题:
我会以相反的顺序来处理这些。
当您使用
data.frame()
创建数据框时,默认情况下会检查组件名称的有效性。从?data.frame
我们得到:这意味着对于 OP
d
,使用创建的将具有这些名称:
来抑制
这种行为可以通过默认行为方式 我们可以做类似
或 之
类的事情,但是如果您想要输入输入时的名称并带有空格怎么办?显然它们不再像以前那样工作:
非标准的、语法上有效的名称在使用时需要加引号,使用反引号
`foo`
或普通引号"foo"
,'foo'
,例如,在
qplot()
示例中,带引号的组件名称无法按预期工作。该图是在字符值“name 1”
和“name 2”
处绘制的,而不是这些变量1 的值
和2
。There are two issues at work here:
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:What this means is that for the the OPs
d
, created usingwill have these names:
This behaviour can be suppressed via
The default behaviour means that we can do things like
or
but what if you want to have the names as you entered them, with the space? Clearly they don't work as before:
Non-standard, syntactically valid names need to be quoted when used, using backticks
`foo`
, or normal quotes"foo"
,'foo'
, e.g.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
and2
.您可以使用反引号将字符串转换为符号:
很高兴听到这有效;)
You can use backticks to turn strings into symbols:
Glad to hear this worked ;)