0

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?

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

@anba: not a problem. Btw I updated my answer to include a more efficient way to read multiple files
@anba: btw, you can select the columns you want directly within readr::read_csv to make it more efficient. See this answer stackoverflow.com/a/50652089

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.