每行合并/加入 data.tables
Merge / Join data.tables per row
我有以下数据 tables,我想从所有三个数据中提取一个数据 table。
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)
现在我想 link 所有 table 在一起,即首先 dt.Ger
,然后可能添加两个空行,然后追加 dt.Aut
,现在再次添加两个空行,最后添加 dt.Den
。理想情况下,如果德国是第一个标题,然后是奥地利(在 dt.Aut
之前的第二个空行)和丹麦(在 dt.Den
之前的第二个空行)。
所以我只有一个 table 作为 return。这个table应该是这样的(我只用SnippingTool做过,所以只是为了解释):
编辑:
使用
l <- list(dt.Ger, dt.Aut, dt.Den)
l.result <- rbindlist(l)
产量为:
而且我想要额外的 space/line/row(在红色部分),其中写有德国、奥地利和丹麦。
我仍然不确定,你想要达到什么目的 - 对我来说,你最好使用 data.tables.
的列表
此外,我改用 dcast
而不是 pivot_wider
,这样您就可以删除 tidyr
/ dplyr
。
但是,这里有一种使用 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)
我有以下数据 tables,我想从所有三个数据中提取一个数据 table。
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)
现在我想 link 所有 table 在一起,即首先 dt.Ger
,然后可能添加两个空行,然后追加 dt.Aut
,现在再次添加两个空行,最后添加 dt.Den
。理想情况下,如果德国是第一个标题,然后是奥地利(在 dt.Aut
之前的第二个空行)和丹麦(在 dt.Den
之前的第二个空行)。
所以我只有一个 table 作为 return。这个table应该是这样的(我只用SnippingTool做过,所以只是为了解释):
编辑: 使用
l <- list(dt.Ger, dt.Aut, dt.Den)
l.result <- rbindlist(l)
产量为:
而且我想要额外的 space/line/row(在红色部分),其中写有德国、奥地利和丹麦。
我仍然不确定,你想要达到什么目的 - 对我来说,你最好使用 data.tables.
的列表此外,我改用 dcast
而不是 pivot_wider
,这样您就可以删除 tidyr
/ dplyr
。
但是,这里有一种使用 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)