1

Let's say I have a tibble data frame called df in R like this:

df <- tibble(a = 1:3, 
             b = c("a", "b", "c"))

It is fairly easy to rename the variables with dplyr::rename() (or create new ones with dplyr::mutate()) including unquoting with the := operator, e.g.:

df <- df %>% 
    rename("the new b" := b) %>%
    mutate(c = a + 1)

Which gives me:

> 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

However, when I want to include math symbols or equations in the variable names with expression(), it doesn't work, e.g. when I try to use the Greek alpha symbol it fails:

# 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: To be clear, in the example above I want to get the actual Greek alpha symbol (not the string of alphabet characters "alpha").

FURTHER EDIT: Here's a complex example. What if I want something like this as the variable name:

complex math equation with big sigma and division

Possible use cases for the complex example are for facet labels when plotting with ggplot2::facet_wrap() or rendering a data frame as a table with rmarkdown, etc....

I've tried nesting expression() within paste() or str_c() to no avail. How do I achieve this? Thank you.

hpy
  • 1,989
  • 7
  • 26
  • 56
  • 1
    Hmm that is a complicated name. But, don't you think it is a bit too difficult to extract values from that column by calling that as column name – akrun Sep 04 '18 at 17:46
  • It would be hard, but I was thinking in terms of when I have to produce final figures for an analyses, exporting to a fully rendered table, etc. So there will be no more calls to the data frame after that... – hpy Sep 04 '18 at 17:48
  • 1
    If the unicode values are available, may be you can create the expression with `paste` – akrun Sep 04 '18 at 17:49
  • 1
    i.e. `df %>% mutate(!! paste0("f(x) = ", "\u03B1", "/", "\u03C1") := c)` I don't have the symbol code at the moment – akrun Sep 04 '18 at 17:50
  • 1
    Something like `df %>% mutate(!! paste0("f(x) = ", "\u03A3", "xi/i") := c)` – akrun Sep 04 '18 at 17:52
  • 1
    True. I think that's certainly more than good enough for the majority of my use cases. Thank you for your suggestions! Marking as solved. – hpy Sep 04 '18 at 17:58

1 Answers1

1

We can convert it to a symbol or character and then do the := after evaluating (!!)

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

where

expr <- expression(A ~ symbol:~ alpha)

If we want the greek letter (as @hpy commented), use the unicode character -for alpha it is \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

The above can also be extended to include some expressions

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
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thanks @akrun for your quick response! Sorry I wasn't clear enough in my original question (I'll edit it), but I was hoping to get the actual Greek alpha symbol as part of the variable name. Using `!!` with `as.character()` did not do that for me, even after trying exporting the data frame to a CSV or .xlsx. Is there a way to do that? – hpy Sep 04 '18 at 16:53
  • 1
    @hpy Looks like you need unicode characters to evaluate – akrun Sep 04 '18 at 17:07
  • Thanks @akrun: Does that mean I simply find the unicode codes for whatever symbol I want to use and put it in `expression()`? If so, how can equations be handled? – hpy Sep 04 '18 at 17:08
  • 1
    @hpy You can check [here](https://stackoverflow.com/questions/17761858/converting-a-u-escaped-unicode-string-to-ascii) – akrun Sep 04 '18 at 17:10
  • 1
    OK, I just tried `mutate(!!paste0("\u03B1") := c )` and it worked! An actual Greek alpha is now the new variable's name. Thank you! I wonder, however, if there is a way to do actual math equations with `expression()`?... – hpy Sep 04 '18 at 17:31
  • 1
    @hpy Do you need something like `df %>% mutate(!! paste0("\u03B1", "+", "\u03C1") := c)` – akrun Sep 04 '18 at 17:39
  • I've added a complicated example in the original question. – hpy Sep 04 '18 at 17:45