I have mutliple csv files (92 files belonging to 92 samples) in a directory. Each csv file has different number of rows but the same set of columns. Each csv file is metagenomic microbial abundance data from Bracken. To work on all the files at the same time , i am using lapply. After setting the working directory to the folder that only has the csv files,
fnames <- list.files()
myfiles = lapply(fnames, read.delim) # I am reading each csv file into a data frame here within the list.
I want to filter out all the columns except "name" and "fraction_total_reads".
lst1 <- lapply(myfiles, "[", c("name", "fraction_total_reads"))
So far so good. Now i have 92 csv files with different number of rows, but with the same exact 2 columns - "name" and "fraction_total_reads".
I want to write each data frame within the list lst1 into a separate csv file. Right now I am doing this.
write.csv(file=paste("df1.csv"), lst1[[1]], row.names = FALSE)
write.csv(file=paste("df2.csv"), lst1[[2]], row.names = FALSE)
write.csv(file=paste("df3.csv"), lst1[[3]], row.names = FALSE)
Like this, now I have to type in 92 lines. Is there a way to save this list of df into different csv file with a single line code?
Thank you. This code worked.
iwalk(lst1, ~write.csv(.x, str_c("df", .y, ".csv"), row.names=FALSE))
I appreciate the help.