Combine Multiple .Rdata Files Containing Objects with the Same Name into One Single .Rdata File

Combine all Rdata files in directory with a same name object in it

If I understood it correctly you want to get all that .RData into one single data.frame.

One option is to list all files in your working directory that have the extension .RData, load and combine them using rbind:

ll <- list.files(pattern = '.RData')

res <- do.call(rbind,
lapply(ll, function(x) {

load(file = x)
get(ls())
}))

No we can check both top 6 rows.

head(res)

# chrom start end gid gname tid strand
#32590 chr7 45574608 45574777 ENSMUSG00000085214 0610005C13Rik ENSMUST00000130094 -
#109006 chr4 154023688 154023891 ENSMUSG00000078350 1190007F08Rik ENSMUST00000143047 -
#475764 chr15 83365029 83365513 ENSMUSG00000075511 1700001L05Rik ENSMUST00000178628 -
#448806 chr13 31567474 31567610 ENSMUSG00000038408 1700018A04Rik ENSMUST00000150418 -
#11159 chr6 147694981 147695041 ENSMUSG00000085077 1700049E15Rik ENSMUST00000152737 +
#339243 chr12 22958352 22960254 ENSMUSG00000073164 2410018L13Rik ENSMUST00000149246 -
# class biotype byname.uniq bygid.uniq
#32590 altAcceptor lincRNA TRUE TRUE
#109006 altAcceptor lincRNA TRUE TRUE
#475764 altAcceptor lincRNA TRUE TRUE
#448806 altAcceptor lincRNA TRUE TRUE
#11159 altAcceptor lincRNA TRUE TRUE
#339243 altAcceptor lincRNA TRUE TRUE

and bottom 6 too:

tail(res)
# chrom start end gid gname tid strand
#189235 chr6 90373711 90373841 ENSMUSG00000034430 Zxdc ENSMUST00000113539 +
#563026 chr11 72916473 72916587 ENSMUSG00000055670 Zzef1 ENSMUST00000069395 +
#563046 chr11 72916473 72916587 ENSMUSG00000055670 Zzef1 ENSMUST00000152481 +
#158407 chr3 152449013 152449128 ENSMUSG00000039068 Zzz3 ENSMUST00000106101 +
#158450 chr3 152449013 152449128 ENSMUSG00000039068 Zzz3 ENSMUST00000106103 +
#158465 chr3 152449016 152449128 ENSMUSG00000039068 Zzz3 ENSMUST00000089982 +
# class biotype byname.uniq bygid.uniq
#189235 altAcceptor protein_coding FALSE FALSE
#563026 altAcceptor protein_coding FALSE FALSE
#563046 altAcceptor protein_coding FALSE FALSE
#158407 altAcceptor protein_coding FALSE FALSE
#158450 altAcceptor protein_coding FALSE FALSE
#158465 altAcceptor protein_coding FALSE FALSE

and you can check the dimensions.

dim(res)
#24279 11

Edit

This worked on R 4.0.3. It seems that if fails for R 4.1.1.. I'll edit the answer with a new solution.

How to merge three .RData files with same object name in R

You will need to save them as new objects as you load each, then do your merging. For example, here, create a list to hold the objects. Then, as you load each, add that version to your list.

listForFiles <- list()

load("file1.RData")
listForFiles[[1]] <- A

load("file2.RData")
listForFiles[[2]] <- A

load("file3.RData")
listForFiles[[3]] <- A

Then, you can use listForFiles to do your merging. Since you don't say what kind of object these are, I can't suggest an approach.

How to open multiple .RDATA and save one of there names as data.frame

My list of file names are pretty simple, "mt.Rdata" and "mt1.Rdata"

The code I have for having dataframes for every file is

files <- list.files(getwd())

for(i in files){
print(i)
df <- load(i)
assign(gsub("\\..*","", i), df ) #extracts the string before period
rm(df)
}

Let me know if this is what your are looking for.

How to load multiple .RData in R and them combine as mcmc list?

We can load all the objects into the global env after extracting the object names , then loop over the list, apply mcmc individually on the list elements and then wrap with the mcmc.list

files <- list.files(pattern = "^sample\\s*\\d+\\.RData$")
mcmc.list(lapply(files, function(dat) mcmc(get(load(dat))))

Load multiple RData files efficiently

You can list all the files and read them into a list. Then bind them at the end.

my_files <- list.files(path = "Data/", pattern = "*.RData", full.names = T)

all_data <- lapply(my_files, load, .GlobalEnv)

bind_rows(mget(unlist(all_data)))

A better solution than using mget is defining a new.env to load the files there and then read them into a list:

my_files <- list.files(path = "Data/", pattern = "*.RData", full.names = T)

temp <- new.env()
lapply(my_files, load, temp)

all_data <- as.list(temp)
rm(temp)

bind_rows(all_data)

Loading muliplte .rdata files into one dataframe

Within function f create a new.environment load it there and get it in a list. Finally lapply over it and rbind.

f <- \(x) {
fenv <- new.env()
load(x, envir=fenv)
get(ls(fenv), envir=fenv)
}

do.call(rbind, lapply(list.files(pattern="*.RData"), f))
# X1 X2 X3 X4
# 1 1 4 7 10
# 2 2 5 8 11
# 3 3 6 9 12
# 4 1000 4000 7000 10000
# 5 2000 5000 8000 11000
# 6 3000 6000 9000 12000

Note: For the future you should consider using saveRDS instead of save, which allows to assign the read data to an object. (You can only use it to save a single object, though).


Data:

dat <- data.frame(matrix(1:12, 3, 4))
save(dat, file='test1.RData')

dat <- data.frame(matrix(1:12, 3, 4))*1000
save(dat, file='test2.RData')

rm(dat)

R_loading R data files in a loop and assigning a same variable name

Look at assign..

Edit:

In that case look at get function:

> a <- 5
> get("a")
[1] 5

How to read data files, save and delete one by one in R

Try this:

csv2rdata <- function(filename) {
nm <- sub("\\.csv$", "", basename(filename))
assign(nm, data.table::fread(filename), envir = environment())
save(list = nm, file = paste0(dirname(filename), "/", nm, ".RData"), envir = environment())
}

dir("C:\\Data", pattern = "*.csv", full.names = TRUE) %>% lapply(csv2rdata)


Related Topics



Leave a reply



Submit