Re: [R] Renaming multiple objects
Thanks to all who responded and helped. It worked great! On 2021/11/20 下午 10:31, Rui Barradas wrote: Hello, You can get all objects to be changed into a list, change the list's names attribute and assign back to the globalenv. Something like the following. First see if the objects exist in the global env. ls() #[1] "meb1.p.emb" "meb2.p.emb" "mec1.p.emb" "mec2.p.emb" #[5] "mej12.p.emb" "mej22.p.emb" Now the code to change their names # create a vector of names of the objects # whose names are to be changed obj_names <- ls(pattern = "\\.emb$") # this is instruction is not strictly needed # it's meant to check if the regex works (it does) sub("\\.emb$", "", obj_names) #[1] "meb1.p" "meb2.p" "mec1.p" "mec2.p" "mej12.p" "mej22.p" # get the objects into a list tmp_list <- mget(obj_names, envir = .GlobalEnv) # change the list's names names(tmp_list) <- sub("\\.emb$", "", obj_names) # assign them to the global environment list2env(tmp_list, envir = .GlobalEnv) # clean up rm(tmp_list) rm(list = obj_names) # check to see if it worked (it did) ls() #[1] "meb1.p" "meb2.p" "mec1.p" "mec2.p" "mej12.p" "mej22.p" Hope this helps, Rui Barradas Às 10:27 de 20/11/21, Steven Yen escreveu: I have named NUMEROUS objects (each containing, e.g., 48 obs. of 5 variables), such as mec1.p.emb mec2.p.emb meb1.p.emb meb2.p.emb mej12.p.emb mej22.p.emb How would I rename these objects removing the silly ".emb", into objects mec1.p mec2.p meb1.p meb2.p mej12.p mej22.p Thank you! __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Renaming multiple objects
Agreed... This Is The Way. To interactively sit at the R console and type in commands without keeping those commands in an editor and running them from scratch periodically to insure that they achieve your goals is a only a recipe for mystery, not for data analysis. R is a tool for transforming raw data into i interpretable forms. If you are not keeping track of how you used it, how can you convince yourself or others that you did it right? And if you are keeping track, then you should feel comfortable editing that set of transformation steps so they do the right thing the first time. On November 20, 2021 2:53:05 AM PST, Duncan Murdoch wrote: >On 20/11/2021 5:27 a.m., Steven Yen wrote: >> I have named NUMEROUS objects (each containing, e.g., 48 obs. of 5 >> variables), such as >> mec1.p.emb >> mec2.p.emb >> meb1.p.emb >> meb2.p.emb >> mej12.p.emb >> mej22.p.emb >> >> How would I rename these objects removing the silly ".emb", into objects >> mec1.p >> mec2.p >> meb1.p >> meb2.p >> mej12.p >> mej22.p >> > >Use a global search and replace on your source code, and create them >with the correct names. > >Duncan Murdoch > >__ >R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code. -- Sent from my phone. Please excuse my brevity. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Renaming multiple objects
Hello, You can get all objects to be changed into a list, change the list's names attribute and assign back to the globalenv. Something like the following. First see if the objects exist in the global env. ls() #[1] "meb1.p.emb" "meb2.p.emb" "mec1.p.emb" "mec2.p.emb" #[5] "mej12.p.emb" "mej22.p.emb" Now the code to change their names # create a vector of names of the objects # whose names are to be changed obj_names <- ls(pattern = "\\.emb$") # this is instruction is not strictly needed # it's meant to check if the regex works (it does) sub("\\.emb$", "", obj_names) #[1] "meb1.p" "meb2.p" "mec1.p" "mec2.p" "mej12.p" "mej22.p" # get the objects into a list tmp_list <- mget(obj_names, envir = .GlobalEnv) # change the list's names names(tmp_list) <- sub("\\.emb$", "", obj_names) # assign them to the global environment list2env(tmp_list, envir = .GlobalEnv) # clean up rm(tmp_list) rm(list = obj_names) # check to see if it worked (it did) ls() #[1] "meb1.p" "meb2.p" "mec1.p" "mec2.p" "mej12.p" "mej22.p" Hope this helps, Rui Barradas Às 10:27 de 20/11/21, Steven Yen escreveu: I have named NUMEROUS objects (each containing, e.g., 48 obs. of 5 variables), such as mec1.p.emb mec2.p.emb meb1.p.emb meb2.p.emb mej12.p.emb mej22.p.emb How would I rename these objects removing the silly ".emb", into objects mec1.p mec2.p meb1.p meb2.p mej12.p mej22.p Thank you! __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Renaming multiple objects
On 20/11/2021 5:27 a.m., Steven Yen wrote: I have named NUMEROUS objects (each containing, e.g., 48 obs. of 5 variables), such as mec1.p.emb mec2.p.emb meb1.p.emb meb2.p.emb mej12.p.emb mej22.p.emb How would I rename these objects removing the silly ".emb", into objects mec1.p mec2.p meb1.p meb2.p mej12.p mej22.p Use a global search and replace on your source code, and create them with the correct names. Duncan Murdoch __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Renaming multiple objects
I have named NUMEROUS objects (each containing, e.g., 48 obs. of 5 variables), such as mec1.p.emb mec2.p.emb meb1.p.emb meb2.p.emb mej12.p.emb mej22.p.emb How would I rename these objects removing the silly ".emb", into objects mec1.p mec2.p meb1.p meb2.p mej12.p mej22.p Thank you! __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.