Search code examples
rparsingvariablesexpressionnaming

R - How to include symbols/equations in data frame variable names?


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.


Solution

  • 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