我已经将大量的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")
您正在使用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
选择开始。