There are many teachers filled their information in the csv columns, however, these csv files were labelled with a irregular name (e.g., 1.csv, apple school.csv, 003.csv, etc.).
These files contain the same columns (id, score, grade,class, school, city)
I'd like to rename the csv files in a regular way with what they filled in the columns in one go, such as "1A_S_L.csv" then I can recognize this file is from the teacher of 1A, S school, L city
Many thanks for your assistance.
CodePudding user response:
Place your input files in a folder named in
, create an empty folder named out
.
Then something like this will loop over the files in in
, and copy them to files in out
with names based on grade
, class
, school
and city
:
for (file_in in dir("in", full.names = TRUE)) {
df <- read.csv(file_in)
filename <- paste(df[1,"grade"], df[1,"class"], df[1,"school"], df[1,"city"], sep = "_")
file_out <- paste0("out/", filename, ".csv")
print(file_out)
write.csv(df, file_out)
}