I would like to create a new column containing plotmath expressions that I later plan to use somewhere else in the analysis pipeline.
Here is a minimal example along with I tried. For example, here I am trying to create a new column called label
that will have a different plotmath expression depending on the value of y
column.
This doesn't seem to work:
# loading needed libraries
library(tidyverse)
# creating a dataframe
df <- data.frame(x = c(1:10), y = c(rep("a", 5), rep("b", 5))) %>%
tibble::as_data_frame(x = .)
# adding a new column with plotmath expression
df %>%
dplyr::mutate(.data = .,
label = dplyr::case_when(
y == "a" ~ paste(list(
"'This is'", "~alpha==", 1
), sep = ""),
y == "b" ~ paste(list(
"'This is'", "~beta==", 2
), sep = "")))
#> Error in mutate_impl(.data, dots): Evaluation error: `y == "a" ~ paste(list("'This is'", "~alpha==", 1), sep = "")`, `y == "b" ~ paste(list("'This is'", "~beta==", 2), sep = "")` must be length 10 or one, not 3.
Created on 2018-06-26 by the reprex package (v0.2.0).