parse(text="cI[W]-6ren">
gpt4 book ai didi

R `parse()` 下标混合数字和字母

转载 作者:行者123 更新时间:2023-12-04 10:13:11 25 4
gpt4 key购买 nike

我正在尝试使用 parse() 在下标中混合字母和数字的 ggplot 方面标签中显示下标。

这个有效:

>parse(text="cI[933]")

还有这个:

>parse(text="cI[W]")

但不是这些:

>parse(text="cI[933W]")
Error in parse(text = "cI[933W]") : <text>:1:7: unexpected symbol
1: cI[933W
^

> parse(text="cI[9W33]")
Error in parse(text = "cI[9W33]") : <text>:1:5: unexpected symbol
1: cI[9W33
^

ggplot 为例:

data(mtcars)
mtcars$cyl2 <- factor(mtcars$cyl, labels = c("cI[123]", "cI[ABC]", "cI[456]"))
qplot(wt, mpg, data = mtcars) + facet_grid(. ~ cyl2, labeller = label_parsed)

这很好用,但是:

data(mtcars)
mtcars$cyl2 <- factor(mtcars$cyl, labels = c("cI[AB3]", "cI[2CD]", "cI[EF1]"))
qplot(wt, mpg, data = mtcars) + facet_grid(. ~ cyl2, labeller = label_parsed)

...没有。

最佳答案

这是失败的,因为根据 R 解析器,2CD 不是合法符号(符号必须以字母字符开头,并且只能包含字母数字 + [._]字符(我认为))。作为一种快速解决方法,您可以使用单引号保护非法符号:

mtcars$cyl2 <- factor(mtcars$cyl, 
labels = c("cI[AB3]", "cI['2CD']", "cI[EF1]"))

引号不会出现在绘图标签中。

来自introduction to R :

Normally all alphanumeric symbols are allowed (and in some countries this includes accented letters) plus ‘.’ and ‘_’, with the restriction that a name must start with ‘.’ or a letter, and if it starts with ‘.’ the second character must not be a digit.

关于R `parse()` 下标混合数字和字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35960919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com