简体   繁体   English

如何从具有不同行数的小标题列表中创建小标题?

[英]How to create a tibble from list of tibbles with different number of rows?

I have a list of tibbles that look like this:我有一个看起来像这样的小标题列表:

> head(temp)
$AT
# A tibble: 8,784 × 2
   price_eur datetime           
       <dbl> <dttm>             
 1      50.9 2021-01-01 00:00:00
 2      48.2 2021-01-01 01:00:00
 3      44.7 2021-01-01 02:00:00
 4      42.9 2021-01-01 03:00:00
 5      40.4 2021-01-01 04:00:00
 6      40.2 2021-01-01 05:00:00
 7      39.6 2021-01-01 06:00:00
 8      40.1 2021-01-01 07:00:00
 9      41.3 2021-01-01 08:00:00
10      44.9 2021-01-01 09:00:00
# … with 8,774 more rows

$IE
# A tibble: 7,198 × 2
   price_eur datetime           
       <dbl> <dttm>             
 1      54.0 2021-01-01 01:00:00
 2      53   2021-01-01 02:00:00
 3      51.2 2021-01-01 03:00:00
 4      48.1 2021-01-01 04:00:00
 5      47.3 2021-01-01 05:00:00
 6      47.6 2021-01-01 06:00:00
 7      45.4 2021-01-01 07:00:00
 8      43.4 2021-01-01 08:00:00
 9      47.8 2021-01-01 09:00:00
10      51.8 2021-01-01 10:00:00
# … with 7,188 more rows

$`IT-Calabria`
# A tibble: 8,736 × 2
   price_eur datetime           
       <dbl> <dttm>             
 1      50.9 2021-01-01 00:00:00
 2      48.2 2021-01-01 01:00:00
 3      44.7 2021-01-01 02:00:00
 4      42.9 2021-01-01 03:00:00
 5      40.4 2021-01-01 04:00:00
 6      40.2 2021-01-01 05:00:00
 7      39.6 2021-01-01 06:00:00
 8      40.1 2021-01-01 07:00:00
 9      41.3 2021-01-01 08:00:00
10      41.7 2021-01-01 09:00:00
# … with 8,726 more rows

The number of rows is different because there are missing observations, usually one or several days.行数不同,因为缺少观测值,通常是一天或几天。

Ideally I need a tibble with a single date time index and corresponding columns with NAs when there is missing data and I'm stuck here.理想情况下,当缺少数据并且我被困在这里时,我需要一个带有单个日期时间索引的小标题和带有 NA 的相应列。

We can do a full join by 'datetime'我们可以通过“日期时间”进行完全加入

library(dplyr)
library(purrr)
reduce(temp, full_join, by = "datetime")

If we need to rename the column 'price_eur' before the join, loop over the list with imap , rename the 'price_eur' to the corresponding list name ( .y ) and do the join within reduce如果我们需要在连接之前rename列“price_eur”,请使用imap遍历listrename “price_eur”重命名为相应的列表名称( .y )并在reduce中进行连接

imap(temp, ~ .x %>%
              rename(!! .y := price_eur)) %>%
          reduce(full_join, by = 'datetime')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何迭代地将一个小标题的行添加到小标题列表中的小标题? - How to iteratively add rows of one tibble to tibbles within a list of tibbles? R dplyr - 使用 list_cbind() 将具有不同行数的小标题列表组合成一个小标题 - R dplyr - combining a list of tibbles with different number of rows into a single tibble with list_cbind() 将具有不同行数的小标题列表加入一个 dataframe - Join list of tibbles with different number of rows into one dataframe 如何比较具有不同行数的两个小标题之间的多个观察结果 - How to compare multiple observations between two tibbles with different number of rows 使用循环绑定嵌套列表中的 tibbles 行,无论 tibbles 的数量是多少 - Bind rows of tibbles in a nested list with a loop, whatever the number of tibbles 使用每个小标题中的名称从小标题列表中选择变量 - Select variables from list of tibbles using names in each tibble 如何总结列表中的不同小标题? - How to sum different tibbles in a list? 如何通过小标题列表循环脚本并将它们加入一个小标题? - How to loop a script through a list of tibbles and join them into one tibble? 如何在小标题中取消嵌套小标题? - How to unnest tibbles within a tibble? 在嵌套 tibbles 的 tibble 中(在 list-columns 内),如何更新 tibbles 以重命名具有通用名称的列? - In a tibble nesting tibbles (inside list-columns), how to update tibbles to rename columns with a common name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM