在R:如何加入具有相似名称的所有数据框?

时间:2018-06-11 15:10:55

标签: r date dataframe merge

目前,使用for loop,我会使用名称

吐出多个数据框
res_[Insert some date] 

例如在我的环境中,我有:

res_2018-04-01

res_2018-05-01

res_2018-06-01

每个数据帧都有1行和25列。第一列是文件上载的日期(因此对于第一个数据框,列的值为2018-04-11),后续列是文件上载日期之前的24个月。

在for循环运行之后,我想合并所有创建的数据帧,而无需手动输入这些数据帧的名称。例如,我可以使用res_%

之类的名称合并所有数据框

所以我的最终数据框将有27列,并且(在本例中)为3行。

有什么建议吗?

编辑:这是dput的输出(res_2018-04-11)

dput(`res_2018-04-11`)
structure(list(UploadDate = "2018-04-11", `2016-03-01` = 4822598.18735351, 
`2016-04-01` = 4022970.75519652, `2016-05-01` = 4471569.0873137, 
`2016-06-01` = 4762693.19167908, `2016-07-01` = 3799649.58966077, 
`2016-08-01` = 4667486.94228869, `2016-09-01` = 4748252.38636671, 
`2016-10-01` = 3953585.1499195, `2016-11-01` = 5258338.05699641, 
`2016-12-01` = 4529140.27998058, `2017-01-01` = 3334021.87730489, 
`2017-02-01` = 4842435.58785495, `2017-03-01` = 5430798.18389053, 
`2017-04-01` = 4045150.70691188, `2017-05-01` = 4660901.62683975, 
`2017-06-01` = 4684489.25953388, `2017-07-01` = 3680375.93521103, 
`2017-08-01` = 5231564.19023014, `2017-09-01` = 4073906.09821191, 
`2017-10-01` = 5440655.92109229, `2017-11-01` = 4996844.57817061, 
`2017-12-01` = 5087355.28846096, `2018-01-01` = 2895616.00464724, 
`2018-02-01` = 3766770.55063743), .Names = c("UploadDate", 
"2016-03-01", "2016-04-01", "2016-05-01", "2016-06-01", "2016-07-01", 
"2016-08-01", "2016-09-01", "2016-10-01", "2016-11-01", "2016-12-01", 
"2017-01-01", "2017-02-01", "2017-03-01", "2017-04-01", "2017-05-01", 
"2017-06-01", "2017-07-01", "2017-08-01", "2017-09-01", "2017-10-01", 
"2017-11-01", "2017-12-01", "2018-01-01", "2018-02-01"), row.names = c(NA, 
-1L), class = "data.frame")

1 个答案:

答案 0 :(得分:2)

在您的问题中提供最小的工作示例通常很有用。这是一个例子。在这里,我只使用了3列,而不是24列数据,但它显示了相同的一点:

`res_2018-04-11` <- structure(list(UploadDate = "2018-04-11", 
`2017-12-01` = 5087355.28846096, `2018-01-01` = 2895616.00464724, 
`2018-02-01` = 3766770.55063743), .Names = c("UploadDate", "2017-12-01", 
"2018-01-01", "2018-02-01"), row.names = c(NA, -1L), class = "data.frame")

`res_2018-03-09` <- structure(list(UploadDate = "2018-03-09", 
`2017-11-01` = 4996844.57817061, `2017-12-01` = 5087355.28846096, 
`2018-01-01` = 2895616.00464724), .Names = c("UploadDate", "2017-11-01", 
"2017-12-01", "2018-01-01"), row.names = c(NA, -1L), class = "data.frame")

`res_2018-02-12` <- structure(list(UploadDate = "2018-02-12", 
`2017-10-01` = 5440655.92109229, `2017-11-01` = 4996844.57817061, 
`2017-12-01` = 5087355.28846096), .Names = c("UploadDate", "2017-10-01", 
"2017-11-01", "2017-12-01"), row.names = c(NA, -1L), class = "data.frame")

然后,正如@Gregor在评论中提到的那样,创建一个数据帧列表,指定pattern参数:

df_list <- mget(ls(pattern = "^res_.*"))

现在使用rbind.fill包中的plyr

plyr::rbind.fill(df_list)
#   UploadDate 2017-10-01 2017-11-01 2017-12-01 2018-01-01 2018-02-01
# 1 2018-02-12    5440656    4996845    5087355         NA         NA
# 2 2018-03-09         NA    4996845    5087355    2895616         NA
# 3 2018-04-11         NA         NA    5087355    2895616    3766771