简体   繁体   English

R-考虑到某些被调用的数据框可能不存在,如何合并/绑定多个数据框?

[英]R- How to merge/bind several dataframes considering that some of the called dataframes might not exist?

I have to bind/merge dataframes in an iterative process in R.我必须在 R 的迭代过程中绑定/合并数据帧。 In occasions, some of the dataframes might not exist for reasons I will not explain here.在某些情况下,某些数据帧可能不存在,原因我不会在这里解释。 I would like to know how I could bind dataframes even when calling some dataframe that does not exist.我想知道即使在调用一些不存在的 dataframe 时如何绑定数据帧。 As an example, I have these dataframes:例如,我有这些数据框:

df.R_H <- data.frame(A=c(0,4,5,6,7),
                  B=c(3,9,4,5,8),
                  C=c(1,2,4,2,6))

df.B_C <- data.frame(A=c(0,4,5,6,7),
                  B=c(3,9,4,5,8),
                  C=c(1,2,4,2,6))

df.G_H <- data.frame(A=c(0,4,5,6,7),
                  B=c(3,9,4,5,8),
                  C=c(1,2,4,2,6))

However, in my code I have this:但是,在我的代码中,我有这个:

df5 <- rbind(df.R_H,df.B_C,df.G_H,df.R_C)
df5

How can I bind dataframes although df.R_C does not exist?尽管df.R_C不存在,我如何绑定数据帧? I can not check individually which dataframes exists or not since I have hundreds of situations like this and I want to do it automatically even when some dataframe does not exist.我无法单独检查是否存在哪些数据帧,因为我有数百种这样的情况,即使某些 dataframe 不存在,我也想自动执行此操作。

You can use mget() to get rbind() all data frames found in the environment.您可以使用mget()来获取rbind()在环境中找到的所有数据帧。 Alternatively for your example you could pass in x explicitly to mget() as paste0("df", 1:4) instead of ls() .或者,对于您的示例,您可以将x显式传递给mget()作为paste0("df", 1:4)而不是ls()

df1 <- data.frame(A=c(0,4,5,6,7),
                  B=c(3,9,4,5,8),
                  C=c(1,2,4,2,6))

df2 <- data.frame(A=c(0,4,5,6,7),
                  B=c(3,9,4,5,8),
                  C=c(1,2,4,2,6))

df3 <- data.frame(A=c(0,4,5,6,7),
                  B=c(3,9,4,5,8),
                  C=c(1,2,4,2,6))

df5 <- do.call(rbind, mget(ls(), mode = "list", ifnotfound = list(NULL)))
df5
#>       A B C
#> df1.1 0 3 1
#> df1.2 4 9 2
#> df1.3 5 4 4
#> df1.4 6 5 2
#> df1.5 7 8 6
#> df2.1 0 3 1
#> df2.2 4 9 2
#> df2.3 5 4 4
#> df2.4 6 5 2
#> df2.5 7 8 6
#> df3.1 0 3 1
#> df3.2 4 9 2
#> df3.3 5 4 4
#> df3.4 6 5 2
#> df3.5 7 8 6

If you have a crowded environment, which is likely, then you can utilize the pattern argument of ls() to ensure we only pull in relevant data frames through mget() .如果你有一个拥挤的环境,那么你可以利用ls()pattern参数来确保我们只通过mget()拉入相关的数据帧。 So if all your data frames start with df... , then you could use:因此,如果您的所有数据框都以df...开头,那么您可以使用:

do.call(rbind, mget(ls(pattern = "^df*"), mode = "list", ifnotfound = list(NULL)))

Another possible solution:另一种可能的解决方案:

library(tidyverse)

df.R_H <- data.frame(A=c(0,4,5,6,7),
                     B=c(3,9,4,5,8),
                     C=c(1,2,4,2,6))

df.B_C <- data.frame(A=c(0,4,5,6,7),
                     B=c(3,9,4,5,8),
                     C=c(1,2,4,2,6))

df.G_H <- data.frame(A=c(0,4,5,6,7),
                     B=c(3,9,4,5,8),
                     C=c(1,2,4,2,6))

map_dfr(list("df.R_H","df.B_C","df.G_H","df.R_C"), ~ if (exists(.x)) {get(.x)})

#>    A B C
#> 1  0 3 1
#> 2  4 9 2
#> 3  5 4 4
#> 4  6 5 2
#> 5  7 8 6
#> 6  0 3 1
#> 7  4 9 2
#> 8  5 4 4
#> 9  6 5 2
#> 10 7 8 6
#> 11 0 3 1
#> 12 4 9 2
#> 13 5 4 4
#> 14 6 5 2
#> 15 7 8 6

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

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