How to Rbind All the Data.Frames in Your Working Environment

How to rbind all the data.frames in your working environment?

You can search for objects of data.frame class, and use function mget to retrieve them.

a = b = c = data.frame(x=1:2, y=3, z=1:4)
d = "junk"
e = list(poo="pah")
ls()
# [1] "a" "b" "c" "d" "e"
dfs = sapply(.GlobalEnv, is.data.frame)
dfs
# a b c d e
# TRUE TRUE TRUE FALSE FALSE
do.call(rbind, mget(names(dfs)[dfs]))
# x y z
# a.1 1 3 1
# a.2 2 3 2
# a.3 1 3 3
# a.4 2 3 4
# b.1 1 3 1
# b.2 2 3 2
# b.3 1 3 3
# b.4 2 3 4
# c.1 1 3 1
# c.2 2 3 2
# c.3 1 3 3
# c.4 2 3 4

Combine several data frames in the global environment by row (rbind)

Since you have already read the files in, you can try the following:

do.call(rbind, mget(ls(pattern = "df")))

The ls(pattern = df) should capture all of your "df.1", "df.2", and so on. Hopefully you don't have other things named with the same pattern, but if you do, experiment with a stricter pattern until the command lists just your data.frames.

mget() will bring all of these into a list on which you can use do.call(rbind, ...).

using rbind to combine all data sets the names of all data set start with common characters

Try first obtaining a vector of all matching objects using ls() with the pattern ^test:

dfs <- lapply(ls(pattern="^test"), function(x) get(x))
result <- rbindlist(dfs)

I am taking the suggestion by @Rohit to use rbindlist to make our lives easier to rbind together a list of data frames.

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

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

How do I merge all data frames in the global environment?

Following @Osssan's comments, and assuming that you want to merge everything in your global workspace,

Get the names of the objects and then retrieve the objects themselves into a list:

DF_obj <- lapply(ls(), get)

If you want to merge on all common variables (e.g. if all variable names are unique except the one(s) you want to merge on), then just

Reduce(merge, DF_obj)

should work.

Unfortunately (unlike lapply() etc.) Reduce doesn't have a ... argument for passing additional named arguments to a function, so Reduce(merge, DF_obj, by=common_variable) doesn't work; as @Osssan points out you need something like

mergefun <- function(x, y) merge(x, y, by= "common_variable")
merged_DF <- Reduce(mergefun, DF_obj )

As other commenters point out, if you just kept the data frames in a list in the first place, you could dispense with the ls()/get() step, which is typically clunky/fragile (what if you want to pass the objects back from a function? what if you only want to merge some of the objects in the workspace? ...)

combining multiple data frames from the global environment

Here is one non-loop approach.

#Create name of dataframes
datanames <- paste0('durData_IBM_AskSide', 1:20)
#Combine them into one
combine_data <- do.call(rbind, mget(datanames))
#Remove them from global environment
rm(list = datanames)

rbind data frames based on a common pattern in data frame name

We can use ls with mget

library(data.table)
rbindlist(mget(ls(pattern = "^df\\.\\d+")))

Or with dplyr

library(dplyr)
mget(ls(pattern="^df\\.\\d+")) %>%
bind_rows()

Or with rbind from base R

do.call(rbind, mget(ls(pattern="^df\\.\\d+")))

How can I make a list of all dataframes that are in my global environment?

From your posted code, I would recommend you start a new R session, and read the files in again with the following code

do.call(rbind, lapply(list.files(pattern = ".csv"), read.csv))

Rbind multiple Data Frames in a loop

If file_list is a character vector of filenames that have since been loaded into variables in the local environment, then perhaps one of

do.call(rbind.data.frame, mget(ls(pattern = "^df\\s+\\.csv")))
do.call(rbind.data.frame, mget(paste0("df", seq_along(file_list), ".csv")))

The first assumes anything found (as df*.csv) in R's environment is appropriate to grab. It might not grab then in the correct order, so consider using sort or somehow ordering them yourself.

mget takes a string vector and retrieves the value of the object with each name from the given environment (current, by default), returning a list of values.

do.call(rbind.data.frame, ...) does one call to rbind, which is much much faster than iteratively rbinding.



Related Topics



Leave a reply



Submit