Renaming all Environment objects with a specific name

Asked

Viewed 160 times

2

The Environment is:

inserir a descrição da imagem aqui

but I want to convert into:

inserir a descrição da imagem aqui

where dataset is the specific name.

2 answers

3


An 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]]) ) 
}

3

A 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.