Home > Software design >  Loop code through multiple .csv files in R
Loop code through multiple .csv files in R

Time:10-08

I'm trying to run a chunk of code through 50 csv files, but I can't figure out how to do it. All the csv files have the same columns, but different number of rows.

So far I have the following:

filelist<- list.files(pattern = ".csv") #made a file list with all the .csv files in the directory

samples <- lapply(filelist, function(x) read.table(x, header=T)[,c(1,2,3,5)]) #read all the csv files (only the columns I'm interested in)

output <- samples %>%
      rename (orf = protein) %>%   
      filter (!grepl("sp", orf)) %>% 
      write.csv (paste0("new_", filename))
#I want to rename a column and remove all rows containing "sp" in that column, then export the dataframe as new_originalfilename.csv 

Any help would be greatly appreciated!

CodePudding user response:

You may do this in the same lapply loop.

library(dplyr)

lapply(filelist, function(x) {
  read.table(x, header=T) %>%
    select(1, 2, 3, 5) %>%
    rename(orf = protein) %>%   
    filter(!grepl("sp", orf)) %>%
    write.csv(paste0("new_", x), row.names = FALSE)
})
  •  Tags:  
  • r csv
  • Related