tsibble::fill_gaps 并引用变量中的数字列名
tsibble::fill_gaps and reference to numeric column name in variable
这是我的代码:
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)
但得到了同样的错误。有简单的解决方法吗?我需要让它按规定工作,我不能将数据框列名称更改为非数字的名称。
我们可以使用 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
一般来说,列名不应该是数字,但如果有必要,这里可以使用反引号:
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 23
这是我的代码:
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)
但得到了同样的错误。有简单的解决方法吗?我需要让它按规定工作,我不能将数据框列名称更改为非数字的名称。
我们可以使用 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
一般来说,列名不应该是数字,但如果有必要,这里可以使用反引号:
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 23