R - 如何在数据框变量名称中包含 symbols/equations?
R - How to include symbols/equations in data frame variable names?
假设我在 R 中有一个名为 df
的 tibble
数据框,如下所示:
df <- tibble(a = 1:3,
b = c("a", "b", "c"))
使用 dplyr::rename()
重命名变量(或使用 dplyr::mutate()
创建新变量)相当容易,包括使用 :=
运算符取消引用,例如:
df <- df %>%
rename("the new b" := b) %>%
mutate(c = a + 1)
这给了我:
> df
# A tibble: 3 x 3
a `the new b` c
<int> <chr> <dbl>
1 1 a 2
2 2 b 3
3 3 c 4
但是,当我想在 expression()
的变量名称中包含数学符号或方程式时,它不起作用,例如当我尝试使用希腊字母符号时失败了:
# Fails:
> df <- df %>%
+ mutate(expression(A~symbol:~alpha) = c)
Error: unexpected '=' in:
"df <- df %>%
mutate(expression(A~symbol:~alpha) ="
# Fails again:
> df <- df %>%
+ mutate(expression(A~symbol:~alpha) := c)
Error: The LHS of `:=` must be a string or a symbol
EDIT/UPDATE:要清楚,在上面的例子中我想得到实际的希腊字母符号(不是字母字符串 "alpha").
进一步编辑:这是一个复杂的例子。如果我想要这样的东西 作为变量名怎么办:
复杂示例的可能用例是使用 ggplot2::facet_wrap()
绘制时的 facet
标签或使用 rmarkdown
将数据框渲染为 table 等。 ..
我试过在 paste()
或 str_c()
中嵌套 expression()
但无济于事。我如何实现这一目标?谢谢。
我们可以将其转换为符号或字符,然后在计算 (!!
)
后执行 :=
df %>%
mutate(!! as.character(expr) := c)
# A tibble: 3 x 4
# a `the new b` c `A ~ symbol:~alpha`
# <int> <chr> <dbl> <dbl>
#1 1 a 2 2
#2 2 b 3 3
#3 3 c 4 4
哪里
expr <- expression(A ~ symbol:~ alpha)
如果我们想要希腊字母(正如@hpy 评论的那样),请使用 unicode 字符 - 对于 alpha,它是 \u03B1
df %>%
mutate(!! "\u03B1" := c)
# A tibble: 3 x 4
# a `the new b` c α
# <int> <chr> <dbl> <dbl>
#1 1 a 2 2
#2 2 b 3 3
#3 3 c 4 4
以上还可以扩展为包含一些表达式
df %>%
mutate(!! paste0("\u03B1", "+", "\u03C1") := c)
# A tibble: 3 x 4
# a `the new b` c `α+ρ`
# <int> <chr> <dbl> <dbl>
#1 1 a 2 2
#2 2 b 3 3
#3 3 c 4 4
假设我在 R 中有一个名为 df
的 tibble
数据框,如下所示:
df <- tibble(a = 1:3,
b = c("a", "b", "c"))
使用 dplyr::rename()
重命名变量(或使用 dplyr::mutate()
创建新变量)相当容易,包括使用 :=
运算符取消引用,例如:
df <- df %>%
rename("the new b" := b) %>%
mutate(c = a + 1)
这给了我:
> df
# A tibble: 3 x 3
a `the new b` c
<int> <chr> <dbl>
1 1 a 2
2 2 b 3
3 3 c 4
但是,当我想在 expression()
的变量名称中包含数学符号或方程式时,它不起作用,例如当我尝试使用希腊字母符号时失败了:
# Fails:
> df <- df %>%
+ mutate(expression(A~symbol:~alpha) = c)
Error: unexpected '=' in:
"df <- df %>%
mutate(expression(A~symbol:~alpha) ="
# Fails again:
> df <- df %>%
+ mutate(expression(A~symbol:~alpha) := c)
Error: The LHS of `:=` must be a string or a symbol
EDIT/UPDATE:要清楚,在上面的例子中我想得到实际的希腊字母符号(不是字母字符串 "alpha").
进一步编辑:这是一个复杂的例子。如果我想要这样的东西 作为变量名怎么办:
复杂示例的可能用例是使用 ggplot2::facet_wrap()
绘制时的 facet
标签或使用 rmarkdown
将数据框渲染为 table 等。 ..
我试过在 paste()
或 str_c()
中嵌套 expression()
但无济于事。我如何实现这一目标?谢谢。
我们可以将其转换为符号或字符,然后在计算 (!!
)
:=
df %>%
mutate(!! as.character(expr) := c)
# A tibble: 3 x 4
# a `the new b` c `A ~ symbol:~alpha`
# <int> <chr> <dbl> <dbl>
#1 1 a 2 2
#2 2 b 3 3
#3 3 c 4 4
哪里
expr <- expression(A ~ symbol:~ alpha)
如果我们想要希腊字母(正如@hpy 评论的那样),请使用 unicode 字符 - 对于 alpha,它是 \u03B1
df %>%
mutate(!! "\u03B1" := c)
# A tibble: 3 x 4
# a `the new b` c α
# <int> <chr> <dbl> <dbl>
#1 1 a 2 2
#2 2 b 3 3
#3 3 c 4 4
以上还可以扩展为包含一些表达式
df %>%
mutate(!! paste0("\u03B1", "+", "\u03C1") := c)
# A tibble: 3 x 4
# a `the new b` c `α+ρ`
# <int> <chr> <dbl> <dbl>
#1 1 a 2 2
#2 2 b 3 3
#3 3 c 4 4