下面是我的代码:
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)它会生成错误:
Error: unexpected '=' in:
"test <- my_df %>% as_tsibble(., index=dates) %>%
fill_gaps(., as.character(i) ="我也试过了
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)但得到了相同的错误。有简单的解决方法吗?我需要让它像声明的那样工作,我不能将dataframe列名更改为非数字的名称。
发布于 2020-06-10 06:31:38
我们可以使用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发布于 2020-06-10 06:40:22
一般来说,列名不应该是数字,但在这里,如果有必要,你可以使用反引号:
test <- my_df %>% as_tsibble(., index=dates) %>%
fill_gaps(., `1` = 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 23https://stackoverflow.com/questions/62292839
复制相似问题