R: Save All Data.Frames in Workspace to Separate .Rdata Files

R: Save all data.frames in workspace to separate .RData files

Creating a bunch of different files isn't how save() is vectorized. Probably better to use a loop here. First, get a vector of all of your data.frame names.

dfs<-Filter(function(x) is.data.frame(get(x)) , ls())

Now write each to a file.

for(d in dfs) {
save(list=d, file=paste0(d, ".RData"))
}

Or if you just wanted them all in one file

save(list=dfs, file="alldfs.RData")

Saving dataframes as .Rdata files using a for loop

You need to make the name of your dataset outside of save() as it evaluates the first arguments as character or symbols and functions would not be evaluated.

Also, you need to get rid of quotations around the second paste0 and close your parentheses for save() which look like a typos.

for (i in 1:11){
dbname <- paste0("Dataset_",i)
save(dbname, file = paste0("Hypothesis1/Dataset",i, ".RData"))
}

Better approach would be using apply functions, listing your datasets name using ls, and passing them into save as character using list = ... argument.

lapply(ls(pattern="Dataset[0-9]+"), function(x) save(list = x, file = paste0("Hypothesis1/",x,".RData")))

Saving and loading data.frames

As @mrdwab points out, save saves the names as well as the data/structure (and in fact can save a number of different R objects in a single file). There is another pair of storage functions that behave more as you expect. Try this:

saveRDS(df, file="mytweets.rds")
df2 <- readRDS("mytweets.rds")

These functions can only handle a single object at a time.

Use multiple dataframes from a package of data in R

@rawr's response works perfectly:

df.list <- mget(l.my.package$results[, 'Item'], inherits = TRUE)

In R, is it possible to save the current workspace without quitting?

You can use save.image() at any time to save all environment data into an .RData file:

save.image(file='yoursession.RData')

To load this data later you can use:

load('yoursession.RData')

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.

Saving several variables in a single RDS file

You need to use the list argument of the save function. EG:

var1 = "foo"
var2 = 2
var3 = list(a="abc", z="xyz")
ls()
save(list=c("var1", "var2", "var3"), file="myvariables.RData")
rm(list=ls())
ls()

load("myvariables.RData")
ls()

Please note that the saveRDS function creates a .RDS file, which is used to save a single R object. The save function creates a .RData file (same thing as .RDA file). .RData files are used to store an entire R workspace, or whichever names in an R workspace are passed to the list argument.

YiHui has a nice blogpost on this topic.

If you have several data tables and need them all saved in a single R object, then you can go the saveRDS route. As an example:

datalist = list(mtcars = mtcars, pressure=pressure)
saveRDS(datalist, "twodatasets.RDS")
rm(list=ls())

datalist = readRDS("twodatasets.RDS")
datalist

Creating a file with more than one data frame

The object in question is a list. You can see this by using the following code:

library(BradleyTerry2)
data(flatlizards)
str(flatlizards)

You can see it is a list of 2, and its elements are the separate data frames.

You can combine objects together in a list like this quite easily:

a <- data.frame(x=rnorm(10), y=runif(10))
b <- data.frame(w=rnorm(20), z=runif(20))

ablist <- list(a, b)

From there, you take the approach @mrdwab has taken.

Alternatively, if you want them in one dataframe or one csv, you can try using merge.



Related Topics



Leave a reply



Submit