Data wrangling multiple csv files (92 files) in R using lapply
1
0
Entering edit mode
3.4 years ago
pramach1 ▴ 40

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 • 4.4k views
ADD COMMENT
1
Entering edit mode
3.4 years ago

Base R answer.

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

Or using the tidyverse.

library("tidyverse")

iwalk(lst1, ~write.csv(.x, str_c("df", .y, ".csv"), row.names=FALSE))
ADD COMMENT
0
Entering edit mode

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

I appreciate the help.

ADD REPLY

Login before adding your answer.

Traffic: 2527 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6