作者tibbletime
这里。最近在GitHub上提出了一个关于这个问题。解决方法是使用rlang::new_formula()
预构建公式。如果使用POSIXct日期,我们还需要一个特殊的帮助函数,可以处理在公式中添加+
。
这里是助手:
# Time formula creator
# Can pass character, Date, POSIXct
create_time_formula <- function(lhs, rhs) {
if(!inherits(lhs, c("character", "Date", "POSIXct"))) {
stop("LHS must be a character or date")
}
if(!inherits(rhs, c("character", "Date", "POSIXct"))) {
stop("RHS must be a character or date")
}
if(inherits(lhs, "Date")) {
lhs <- as.character(lhs)
} else if (inherits(lhs, "POSIXct")) {
lhs <- gsub(" ", " + ", lhs)
}
if(inherits(rhs, "Date")) {
rhs <- as.character(rhs)
} else if (inherits(rhs, "POSIXct")) {
rhs <- gsub(" ", " + ", rhs)
}
rlang::new_formula(lhs, rhs)
}
使用助手功能与你的开始和结束的日期版本日期
Data_Start<- as.POSIXct("2015-09-07 01:55:00")
Data_End <- as.POSIXct("2015-09-10 01:59:00")
time_formula <- create_time_formula(Data_Start, Data_End)
create_series(time_formula, 1~M, tz = "UTC")
产地:
# A time tibble: 4,325 x 1
# Index: date
date
<dttm>
1 2015-09-07 01:55:00
2 2015-09-07 01:56:00
3 2015-09-07 01:57:00
4 2015-09-07 01:58:00
5 2015-09-07 01:59:00
6 2015-09-07 02:00:00
7 2015-09-07 02:01:00
8 2015-09-07 02:02:00
9 2015-09-07 02:03:00
10 2015-09-07 02:04:00
# ... with 4,315 more rows
在将来的版本tibbletime
我可能会加入更强大的版本辅助功能。
更新:tibbletime 0.1.0
已经发布了,和更强大的实现允许直接使用公式中的变量。此外,公式的每一边必须是是与索引相同的字符或对象(即2013 ~ 2014
应该是"2013" ~ "2014"
)。
library(tibbletime)
Data_Start<- as.POSIXct("2015-09-07 01:55:00")
Data_End <- as.POSIXct("2015-09-10 01:59:00")
create_series(Data_Start ~ Data_End, "1 min")
#> # A time tibble: 4,325 x 1
#> # Index: date
#> date
#> <dttm>
#> 1 2015-09-07 01:55:00
#> 2 2015-09-07 01:56:00
#> 3 2015-09-07 01:57:00
#> 4 2015-09-07 01:58:00
#> 5 2015-09-07 01:59:00
#> 6 2015-09-07 02:00:00
#> 7 2015-09-07 02:01:00
#> 8 2015-09-07 02:02:00
#> 9 2015-09-07 02:03:00
#> 10 2015-09-07 02:04:00
#> # ... with 4,315 more rows