I am trying to arrange a tibble based on a certain column (but setting the column based on a name dynamically set by a variable).
Below is the code I currently am trying but am getting an error. The second paste of code works (where I have hard coded in the column name of symbol which I want to be set based off of a variable instead).
library(tidyverse)
group_var <- "symbol"
date_seq <- seq(as.Date("2000-01-01"), as.Date("2009-12-31"), by = "days")
test_tbl <- tibble::tibble("date" = rep(date_seq, 3),
"symbol" = rep(c("test3", "test1", "test2"), each = length(date_seq)),
"value" = c(rnorm(length(date_seq), sd = 0.05),
rnorm(length(date_seq), sd = 0.05),
rnorm(length(date_seq), sd = 0.05)))
order_var <- c("test1", "test2", "test3")
test_tbl_final <- test_tbl %>%
dplyr::arrange(factor(!!group_var, levels = order_var), date)
Below is the code that works and shows what I am trying to accomplish:
library(tidyverse)
date_seq <- seq(as.Date("2000-01-01"), as.Date("2009-12-31"), by = "days")
test_tbl <- tibble::tibble("date" = rep(date_seq, 3),
"symbol" = rep(c("test3", "test1", "test2"), each = length(date_seq)),
"value" = c(rnorm(length(date_seq), sd = 0.05),
rnorm(length(date_seq), sd = 0.05),
rnorm(length(date_seq), sd = 0.05)))
order_var <- c("test1", "test2", "test3")
test_tbl_final <- test_tbl %>%
dplyr::arrange(factor(symbol, levels = order_var), date)