Reputation: 583
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?
Upvotes: 0
Views: 766
Reputation: 28331
You can't assign read_csv
results to a string like that. Instead you can just store it in a temporary variable tmp
for (i in 1:10){
tmp <- read_csv(paste0("file_", i, "_tail.csv"))
tmp <- tmp[, c(1:7,30)]
write.csv2(tmp, file = paste0("file_", i, "_selected.csv"))
}
Btw this is probably a more efficient way to read multiple files
library(tidyverse)
filePattern <- "\\.csv$"
fileList <- list.files(path = ".", recursive = FALSE,
pattern = filePattern, full.names = TRUE)
result <- fileList %>%
purrr::set_names(nm = (basename(.) %>% tools::file_path_sans_ext())) %>%
purrr::map_df(read_csv, .id = "FileName") %>%
select(1:7, 30)
result
Upvotes: 1