Search code examples
rtsibble

tsibble::fill_gaps and reference to numeric column name in variable


Here is my code:

library(fpp3)
library(dplyr)
my_df <- data.frame("dates" = as.Date(c("2020-03-01", "2020-03-02", "2020-03-05")), 
                    "col_1" = c(1,2,23))
colnames(my_df) <- c("dates", "1")

i <- 1

test <- my_df %>% as_tsibble(., index=dates) %>% 
  fill_gaps(., as.character(i) = 1)

it generates error:

Error: unexpected '=' in:
"test <- my_df %>% as_tsibble(., index=dates) %>% 
  fill_gaps(., as.character(i) ="

I also tried

test <- my_df %>% as_tsibble(., index=dates) %>% 
  fill_gaps(., !!as.character(i) = 1)

test <- my_df %>% as_tsibble(., index=dates) %>% 
  fill_gaps(., !!quo_name(i) = 1)

but got the same error. Is there easy fix? I need to make it work as stated, I cannot change dataframe column names to something non-numeric.


Solution

  • We could use setNames

    my_df %>%
          as_tsibble(., index=dates) %>% 
          fill_gaps(!!! setNames(1, i))
    # A tsibble: 5 x 2 [1D]
    #  dates        `1`
    #  <date>     <dbl>
    #1 2020-03-01     1
    #2 2020-03-02     2
    #3 2020-03-03     1
    #4 2020-03-04     1
    #5 2020-03-05    23