Modifying multiple CSV files and saving them all as TXT in R

Viewed 287

I have a folder containing several .csv files. I need to delete the first three rows and the very last row of all those .csv files and then save them all as .txt. All the files have the same format so it's always the same rows that I would need to delete. I know how to modify a single dataframe but I do not know how to load, modify and save as txt several dataframes. I am a beginner using R so I do not have examples of things I have tried yet. Any help will be really appreciated!

2 Answers

It's hard to start with stack overflow but the other comments about reproducible examples are worth thinking about for the future. My suggestion would be to write a function that reads, modifies, and writes and then loop it across all the files.

I can't tell exactly how to do this as I can't see your data but something like this should work:

library('tidyverse')

old_paths = list.files(
  path = your_folder,
  pattern = '\\.csv$',
  full.names = TRUE
)

read_write = function(path){

  new_filename = str_replace(
    string = path,
    pattern = '\\.csv$',
    replacement = '.txt'
  )

  read_csv(path) %>% 
    slice(-(1:3)) %>% 
    slice(-n()) %>% 
    write_tsv(new_filename) %>% 
    invisible()
}

lapply(old_paths, read_write)

Let's do this for one data frame, only referencing its file name

input_file = "my_data_1.csv"
data = read.csv(input_file)
# modify
data = data[-(1:3), ] # delete first 3 rows
data = data[-nrow(data), ] # delete last row
# save as .txt
output_file = sub("csv$", "txt", input_file)
write.table(x = data, file = output_file, sep = "\t", row.names = FALSE)

Now we can turn it into a function taking the file name as an argument:

my_txt_convert = function(input_file) {
  data = read.csv(input_file)
  # modify
  data = data[-(1:3), ] # delete first 3 rows
  data = data[-nrow(data), ] # delete last row
  # save as .txt
  output_file = sub("csv$", "txt", input_file)
  write.table(x = data, file = output_file, sep = "\t", row.names = FALSE)
}

Then we call the function on all your files:

to_convert = list.files(pattern='.*.csv')
for (file in to_convert) {
  my_txt_convert(file)
}
# or
lapply(to_convert, my_txt_convert)
Related