I want to convert ALL dataframes from my Environment to a file of type .Rda
. My goal is, after this action, to restart R and load them all at once with a for
loop.
I want to convert ALL dataframes from my Environment to a file of type .Rda
. My goal is, after this action, to restart R and load them all at once with a for
loop.
Here is a more automatic response, if you have many date frames and do not want to write the name of each one manually.
First part explaining how to save all the environment data frames:
# criar dados (apenas dois data frames no environment)
df1 <- data.frame(matrix(runif(12), nrow = 3))
df2 <- data.frame(matrix(runif(12), nrow = 3))
vec <- runif(12)
list <- list(runif(12), runif(12))
# obter os nomes dos data frames do environment
dflist <- names(Filter(is.data.frame, as.list(.GlobalEnv)))
# criar uma nova pasta para salvar os Rda's (showWarnings = FALSE para não mostrar os erros se a pasta já existir)
dir.create('dataframes', showWarnings = FALSE)
# salvar todos os data frames
for(i in dflist) {
saveRDS(object = get(i), file = paste0('dataframes/', i, '.Rda'))
}
Now to read all saved frames automatically:
# lista com os nomes dos data frames
dfNames <- dir('dataframes')
# retirar '.Rda'
objNames <- sub('\.Rda$', '', dfNames)
# ler os data frames
for(i in 1:length(dfNames)) {
assign(objNames[i], readRDS(file = paste0('dataframes/', dfNames[i])))
}
df1 = data.frame(x=c(1:5))
df2 = data.frame(x=c(1:5), y=c(6:10))
df3 = data.frame(x=c(1:8), y=c(9:16), z=c(17:24))
lista_df = list(df1, df2, df3)
saveRDS(lista_df, "lista_df.rds")
lista_df_new = readRDS("lista_df.rds")
for (i in 1:length(lista_df_new)){
assign(paste("df",i, sep=""), value = data.frame(lista_df_new[[i]]) )
}