不规则巢穴整理 [英] Irregular nest tidyverse

查看:59
本文介绍了不规则巢穴整理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题.我想按日期嵌套时间序列,但也包括要嵌套的 x 个较早日期.小例子将清除这个:

让我们创建示例表:

set.seed(13)tibble(date = c(rep("2018-01-31", 3), rep("2018-02-28", 3), rep("2018-03-31", 3), rep("2018-04-30", 3)),form = rep(c("A", "B", "C"), 4),值 = 范数(n = 12),ind = runif(12)) ->表

让我们嵌套它:

tbl %>%巢(-日期)# 小费:4 x 2日期数据<chr><列表>1 2018-01-31 <tibble [3 x 3]>2 2018-02-28 <tibble [3 x 3]>3 2018-03-31 <tibble [3 x 3]>4 2018-04-30 <tibble [3 x 3]>

我喜欢这种数据结构格式(我讨厌普通列表).我想要以下内容:

# tibble: 4 x 2日期数据<chr><列表>1 2018-01-31 <NA>2 2018-02-28 <tibble [6 x 3]>3 2018-03-31 <tibble [6 x 3]>4 2018-04-30 <tibble [6 x 3]>

其中第 2018-02-28 行的数据将包括 1 月和 2 月的数据,而第 2018-03-31 行的数据将包括 2 月和 3 月的数据等等.灵活的解决方案,所以我可以说包含多少个以前的时期会是很好的结果.

解决方案

两种方案比较简单,通过绑定表:

tbl %>%嵌套(-日期)%>%变异(数据2 = map2(数据,滞后(数据),〜安全(bind_rows,否则= NA)(.y,.x)$结果))

<块引用>

# A tibble: 4 x 3日期数据data2<chr><列表><列表>1 2018-01-31 <tibble [3 x 3]><lgl [1]>2 2018-02-28 <tibble [3 x 3]><tibble [6 x 3]>3 2018-03-31 <tibble [3 x 3]><tibble [6 x 3]>4 2018-04-30 <tibble [3 x 3]><tibble [6 x 3]>

I have following problem. I want to nest time series by date but include x amount of earlier dates to nest as well. little example will clear this one:

lets create sample tbl:

set.seed(13)
tibble(date = c(rep("2018-01-31", 3), rep("2018-02-28", 3), rep("2018-03-31", 3), rep("2018-04-30", 3)),
       form = rep(c("A", "B", "C"), 4),
       value = rnorm(n = 12),
       ind = runif(12)) -> tbl

And lets nest it:

tbl %>% 
  nest(-date)

# A tibble: 4 x 2
  date       data            
  <chr>      <list>          
1 2018-01-31 <tibble [3 x 3]>
2 2018-02-28 <tibble [3 x 3]>
3 2018-03-31 <tibble [3 x 3]>
4 2018-04-30 <tibble [3 x 3]>

I love this format of data structure (I hate normal lists). I would like to have following:

# A tibble: 4 x 2
  date       data            
  <chr>      <list>          
1 2018-01-31 <NA>
2 2018-02-28 <tibble [6 x 3]>
3 2018-03-31 <tibble [6 x 3]>
4 2018-04-30 <tibble [6 x 3]>

Where data in row 2018-02-28 would include Jan and Feb data and row 2018-03-31 would include Feb and Mar data and so on. Flexible solution, so I can say how many previous periods to include would be great result.

解决方案

The scenario of two is relatively easy, by binding tables:

tbl %>% 
  nest(-date) %>% 
  mutate(data2 = map2(data, lag(data), ~safely(bind_rows, otherwise = NA)(.y, .x)$result))

# A tibble: 4 x 3
  date       data             data2           
  <chr>      <list>           <list>          
1 2018-01-31 <tibble [3 x 3]> <lgl [1]>       
2 2018-02-28 <tibble [3 x 3]> <tibble [6 x 3]>
3 2018-03-31 <tibble [3 x 3]> <tibble [6 x 3]>
4 2018-04-30 <tibble [3 x 3]> <tibble [6 x 3]>

这篇关于不规则巢穴整理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆