How to rename all Environment dataframes with a specific name [R]

2

The Environment is:

But,Iwanttoconvertto:

where dataset is the specific name.

    
asked by anonymous 22.08.2018 / 15:59

2 answers

3

One option would be like this.

ola = data.frame(x=1)
tudo_bem_com = data.frame(y=2)
voce = data.frame(z=3)

lista_df = list(ola, tudo_bem_com, voce)

for (i in 1:length(lista_df)){
  assign(paste("dataset",i, sep=""), value = data.frame(lista_df[[i]]) ) 
}
    
22.08.2018 / 17:05
3

One way to rename the objects of a environment may be as follows.

renameObject <- function(old, new, envir){
    for(i in seq_along(old)){
        assign(new[[i]], get(old[[i]], envir = envir), envir = envir)
        rm(list = old[[i]], envir = envir)
    }
    invisible(NULL)
}



ls()
#[1] "renameObject"

ola <- data.frame(x = 1)
tudo_bem_com <- data.frame(x = 2)
voce <- data.frame(x = 3)

ls()
#[1] "ola"          "renameObject" "tudo_bem_com" "voce"

renameObject(c("ola", "tudo_bem_com", "voce"), paste0("dataset", 1:3), .GlobalEnv)
ls()
#[1] "dataset1"     "dataset2"     "dataset3"     "renameObject"

dataset1
#  x
#1 1
    
22.08.2018 / 19:53