简体   繁体   English

每行合并/加入 data.tables

[英]Merge / Join data.tables per row

I have the following data tables and I would like to make a single data table out of all three.我有以下数据表,我想从所有三个数据表中制作一个数据表。

library(dplyr)
set.seed(123)

dt.Ger <- data.table(date = seq(as.Date('2020-01-01'), by = '1 day', length.out = 365),
                     Germany = rnorm(365, 2, 1), check.names = FALSE)
dt.Aut <- data.table(date = seq(as.Date('2020-01-01'), by = '1 day', length.out = 365),
                     Austria = rnorm(365, 4, 2), check.names = FALSE)
dt.Den <- data.table(date = seq(as.Date('2020-01-01'), by = '1 day', length.out = 365),
                     Denmark = rnorm(365, 3, 1), check.names = FALSE)

dt.Ger <- dt.Ger %>%
  mutate(month = format(date, '%b'), 
         date = format(date, '%d')) %>%
  tidyr::pivot_wider(names_from = date, values_from = Germany)

dt.Aut <- dt.Aut %>%
  mutate(month = format(date, '%b'), 
         date = format(date, '%d')) %>%
  tidyr::pivot_wider(names_from = date, values_from = Austria)

dt.Den <- dt.Den %>%
  mutate(month = format(date, '%b'), 
         date = format(date, '%d')) %>%
  tidyr::pivot_wider(names_from = date, values_from = Denmark)

Now I would like to link all tables together, ie first dt.Ger , then possibly add two empty lines and then append dt.Aut , now add again two empty lines and finally add dt.Den .现在我想将所有表链接在一起,即首先dt.Ger ,然后可能添加两个空行,然后附加dt.Aut ,现在再次添加两个空行,最后添加dt.Den Ideally, it would be great if Germany were the first headline, then Austria (in the second empty line before dt.Aut ) and then Denmark (in the second empty line before dt.Den ).理想情况下,如果德国是第一个标题,然后是奥地利(在dt.Aut之前的第二个空行中),然后是丹麦(在dt.Aut之前的第二个空行中),那就dt.Den

So that I only have a single table as a return.所以我只有一张桌子作为回报。 This table should look something like this (I only did it with SnippingTool, so it only serves to explain):这个表应该是这样的(我只用 SnippingTool 做的,所以它只是用来解释的):

在此处输入图片说明

EDIT: Using编辑:使用

l <- list(dt.Ger, dt.Aut, dt.Den)
l.result <- rbindlist(l)

yields to:产生:

在此处输入图片说明

And I want to get an extra space/line/row (at the red parts) where Germany, Austria and Denmark is written.我想在德国、奥地利和丹麦的地方多出一个空格/行/行(在红色部分)。

I'm still not sure, what you are trying to achive - for me it seems you are better of working with a list of data.tables.我仍然不确定,你想要达到什么 - 对我来说,你似乎最好使用 data.tables 列表。

Furthermore, I switched to using dcast instead of pivot_wider so you can drop tidyr / dplyr .此外,我改用dcast而不是pivot_wider以便您可以删除tidyr / dplyr

However, here is an approach inserting NA s inbetween the different data.tables using rbindlist :但是,这是一种使用rbindlist在不同 data.tables 之间插入NA的方法:

library(data.table)
set.seed(123)

dt.Ger <- data.table(date = seq(as.Date('2020-01-01'), by = '1 day', length.out = 365),
                     Germany = rnorm(365, 2, 1), check.names = FALSE)
dt.Aut <- data.table(date = seq(as.Date('2020-01-01'), by = '1 day', length.out = 365),
                     Austria = rnorm(365, 4, 2), check.names = FALSE)
dt.Den <- data.table(date = seq(as.Date('2020-01-01'), by = '1 day', length.out = 365),
                     Denmark = rnorm(365, 3, 1), check.names = FALSE)

# or rather date  ~ month?
dt.Ger[, c("month", "date") := list(format(date, '%b'), format(date, '%d'))]
dt.Ger <- dcast(dt.Ger, month ~ date, value.var = "Germany")

dt.Aut[, c("month", "date") := list(format(date, '%b'), format(date, '%d'))]
dt.Aut <- dcast(dt.Aut, month ~ date, value.var = "Austria")

dt.Den[, c("month", "date") := list(format(date, '%b'), format(date, '%d'))]
dt.Den <- dcast(dt.Den, month ~ date, value.var = "Denmark")

# use a list of data.tables:
recommended <- list(Germany = dt.Ger, Austria = dt.Aut, Denmark = dt.Den)

DT <- rbindlist(list(data.table(month = c("", "Germany")), dt.Ger, data.table(month = c("", "Austria")), dt.Aut, data.table(month = c("", "Denmark")), dt.Den), fill = TRUE) # [, V1 := NULL]
DT[,(names(DT)):= lapply(.SD, as.character), .SDcols = names(DT)]
for (j in seq_len(ncol(DT))){
  set(DT, which(is.na(DT[[j]])), j, "")
}

print(DT)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM