如何在r中重命名多个对象?或者如何在r中的名称中用“ - ”识别对象?

问题描述 投票:0回答:1

我已经将大量的CSV文件读入R中,并且愚蠢地将数据框的名称保存为“ - ”符号,例如“new-zealand_kba”。有没有办法用“_”替换大量对象的“ - ”。例如“new-zealand_kba”将成为“new_zealand_kba”。

或者,有没有办法读取大量的CSV文件,其文件名中的“ - ”替换为“_”。

# reading in all the CSV files in the folder "Oceania"
path <- "C:/Users/Melissa/Desktop/Dissertation/DATA/Oceania/"
files <- list.files(path=path, pattern="*.csv")
for(file in files)
{
  perpos <- which(strsplit(file, "")[[1]]==".")
  assign(
    gsub(" ","",substr(file, 1, perpos-1)), 
    read.csv(paste(path,file,sep="")))
}

# merging all the CSV files with the same country
# is it this section of my code which wont run as new-zealand_red etc is not
# recognised properly.
new-zealand1<-full_join(new-zealand_red, new-zealand_kba, by = "Year")
new-zealand2<-full_join(new-zealand1, new-zealand_con, by = "Year")
new-zealand3<-full_join(new-zealand2, new-zealand_rep, by = "Year")

r object renaming
1个回答
1
投票

您正在使用assign来创建您的data.frames。在这里你可以改变名字:

assign(
   gsub("-", "_", gsub(" ","",substr(file, 1, perpos-1))), 
   read.csv(paste(path,file,sep="")))

但是,分配可能不是最佳选项,也许您应该考虑分别存储数据帧列表而不是每个数据帧,例如像这样(未经测试):

list_of_dataframes <- lapply(files, function(file) read.csv(paste(path,file,sep=""))))

然后你的所有数据框都将放在list中。您可以轻松地为此列表指定名称:

names(list_of_dataframes) <- gsub("[ -]", "_", gsub("\\.csv$", "", files)))

如果您不想重新读取数据,并且想要重命名现有数据框,则可以执行以下操作:

 all_obj <- ls()
 for (old_name in all_aobj) {
    new_name <- make.names(old_name) ## get a syntactical correct name
    if (new_name != old_name) {
        assign(new_name, get(old_name))
        rm(old_name)
    }
 }

注意。此代码还将重命名有效的中缀运算符,如%||%,因此建议从更好的all_obj选择开始。

© www.soinside.com 2019 - 2024. All rights reserved.