Question: Data wrangling multiple csv files (92 files) in R using lapply
gravatar for pramach1
9 weeks ago by
pramach10 wrote:

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.

csv files read into a list, then separated as a diff df within that list

I want to filter out all the columns except "name" and "fraction_total_reads".

lst1 <- lapply(myfiles, "[", c("name", "fraction_total_reads"))

 The list/ each df has only 2 columns as we have filtered out the rest. The csv has different number of rows.

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?

R • 249 views
ADD COMMENTlink modified 9 weeks ago by rpolicastro3.2k • written 9 weeks ago by pramach10
gravatar for rpolicastro
9 weeks ago by
Bloomington, IN
rpolicastro3.2k wrote:

Base R answer.

lapply(seq_along(lst1), function(x) {write.csv(lst1[[x]], paste0("df", x, ".csv"), row.names=FALSE)})

Or using the tidyverse.


iwalk(lst1, ~write.csv(.x, str_c("df", .y, ".csv"), row.names=FALSE))
ADD COMMENTlink written 9 weeks ago by rpolicastro3.2k

Thank you. This code worked. iwalk(lst1, ~write.csv(.x, str_c("df", .y, ".csv"), row.names=FALSE))

I appreciate the help.

ADD REPLYlink written 9 weeks ago by pramach10
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 2422 users visited in the last hour