首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将tibble中的复杂列表对象扩展到更大的tibble

将tibble中的复杂列表对象扩展到更大的tibble
EN

Stack Overflow用户
提问于 2020-04-17 21:34:09
回答 1查看 33关注 0票数 0
代码语言:javascript
复制
library(dplyr)
library(fpp2) # for prison dataset
library(hts) # forecasting function

# prepare group time series
prison.gts <- gts(prison/1e3, characters = c(3,1,9),
                  gnames = c("State", "Gender", "Legal",
                             "State*Gender", "State*Legal",
                             "Gender*Legal"))

result_prison <- tidyr::crossing(methods = c('bu', 'comb'), 
                              fmethod = c('arima')) %>%
  mutate(forecast_result = purrr::map2(methods, fmethod, 
                                       ~forecast.gts(prison.gts,method = .x, fmethod = .y)))


result_prison
# A tibble: 2 x 3
  methods fmethod forecast_result
  <chr>   <chr>   <list>         
1 bu      arima   <gts>          
2 comb    arima   <gts> 

result_prison包含一个名为forecast_result的列,该列被定义为列表。

特别是,我感兴趣的是将以下数据从forecast_result列表中提取到一个没有任何“list”对象的大tibble中。

代码语言:javascript
复制
result_prison$forecast_result[[1]]$bts
result_prison$forecast_result[[1]]$histy
result_prison$forecast_result[[1]]$method # this is actually the same value from column methods
result_prison$forecast_result[[1]]$fmethod # this is actually the same value from column fmethods

理想情况下,我认为更好的tibble版本是当list对象扩展的时候,这样我就有了一个大的tibble,有像char,double等常见的类型,没有list对象。

我试过了

代码语言:javascript
复制
result_prison %>%
  hoist(forecast_result, 
        hoisted_method = "method",
        hoisted_fmethod = "fmethod",
        bts = "bts",
        histy = 'histy'
  )

尽管我最终还是得到了tibble中的时间序列对象的列表。

EN

回答 1

Stack Overflow用户

发布于 2020-04-17 22:05:40

如果最后一篇专栏是dataframes,那么这些想法将会起作用。由于不是,我怀疑您需要一次提取一个条目:

代码语言:javascript
复制
result_prison %>% pluck("forecast_result", 1)
result_prison %>% pluck("forecast_result", 2)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61272919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档