I've got let's say 10 csv files with names like as
file_1_tail.csv
file_2_tail.csv
file_3_tail.csv
...
file_10_tail.csv
The only difference in name is in the number (1 to 10). Each has the same structure - 1000 rows and 100 columns.
I need to read them to the R, select specific columns and write as new file. My code for one file is below:
file_2_tail = read_csv("file_2_tail.csv")
file_2_tail_selected = file_2_tail[,c(1:7,30)])
write.csv2(file_2_tail_selected, file = "file_2_selected.csv")
And now I want to use loop to automate this for all ten files.
for (i in 1:10){
file_"i"_tail = read_csv("file_"i"_tail.csv")
file_"i"_tail_selected = file_"i"_tail[,c(1:7,30)]
write.csv2(file_"i"_tail_selected, file = "file_"i"_selected.csv")
}
And of course it doesn't work - i is not readable in this notation. How should I fix this?