我正在尝试创建自己的功能,我想同时返回POSIXct
和numeric
值。我发现唯一的解决方法是使用unnest()
afterfeatures
函数。我们可以简化这个工作流程吗?我想在我的包中包含功能,我不想让用户unnest
每次都在features
.
示例代码
library(feasts, quietly = T, warn.conflicts = F)
library(dplyr, quietly = T, warn.conflicts = F)
library(tidyr, warn.conflicts = F, quietly = T)
df <- data.frame(datetime = seq(c(ISOdate(2000,3,20)),
by = "hour",
length.out = 30),
group = c(rep("A", 15), rep("B", 15)),
x = rnorm(30))
feat_event <- function(x){
start <- dplyr::first(x)
end <- dplyr::last(x)
length <- difftime(end, start, units = "h")
output <- list(
start = unname(start),
end = unname(end),
length = unname(length)
)
output
}
feat_event(df$datetime)
#> $start
#> [1] "2000-03-20 12:00:00 GMT"
#>
#> $end
#> [1] "2000-03-21 17:00:00 GMT"
#>
#> $length
#> Time difference of 29 hours
df %>%
as_tsibble(key = group, index = datetime) %>%
features(datetime, feat_event) %>%
unnest()
#> Warning: `cols` is now required when using unnest().
#> Please use `cols = c(start, end, length)`
#> # A tibble: 2 x 4
#> group start end length
#> <chr> <dttm> <dttm> <drtn>
#> 1 A 2000-03-20 12:00:00 2000-03-21 02:00:00 14 hours
#> 2 B 2000-03-21 03:00:00 2000-03-21 17:00:00 14 hours
由reprex 包于 2021-03-17 创建(v1.0.0)