简体   繁体   中英

How to export multiple list of dataframes from R

I want to export all dataframes from my list of 3 lists to separate '.txt' files. I have a list 'my_list' containing 3 lists (list1, list2 and list3).

Each list contain multiple dataframes. I want to export all dataframes for the 3 lists to separate files ( r1_df1.txt , r1_df2.txt , r2_df3.txt , r2_df4.txt , r2_df5.txt , r3_df6.txt , r3_df7.txt , r3_df8.txt )

Let create some dataframes:

df1 <- data.frame(geneName = 1:3, EnsemblID = 4:6, position = c(654654654,654654,654654))
df2 <- data.frame(id = 1:3, sex = c("M", "F", "T"))
df3 <- data1 <- data.frame(x1 = 1:3, x2 = letters[1:3])
df4 <- data.frame(y1 = c(3, 2, 1), y2 = c(6, 5, 4))
df5 <- data.frame(geneName = 1:3, EnsemblID = 4:6, position = c(65465,654654,987987))
df6 <- data.frame(id = 1:3, sex = c("C", "S", "T"))
df7 <- data1 <- data.frame(x1 = 1:3, x2 = letters[1:3])
df8 <- data.frame(z1 = c(3, 45, 1), p2 = c(6, 5, 4))

dataframes in a list

list1 <- list(df1, df2); names(list1) <- c("df1", "df2")
list2 <- list(df3,df4,df5); names(list2) <- c("df3", "df4", "df5")
list3 <- list(df6, df7, df8); names(list3) <- c("df6", "df7", "df8")

my_list contain the 3 lists

my_list <- list(list1, list2, list3); names(my_list) <- c("r1", "r2", "r3")

Export dataframes to separate .txt files**

for one list I use this code

lapply(seq_along(list1),
       function(i) write.table(list1[[i]], 
                               paste0("r1_", names(list1)[i] , ".txt"),
                               row.names = FALSE, quote = FALSE, sep = "\t", dec = ","))

## r1_df1.txt
## r1_df2.txt
                           

How can I do the same thing for all lists at once?

I tried for loop

for (i in seq_along(my_list)) {
    filename = paste("r_", names(my_list)[i], ".txt")
    write.table(my_list[[i]], filename, row.names = FALSE, quote = FALSE, sep = "\t", dec = "," )
}
    

I get 3 files, each one with bind dataframes and not separate dataframes.

It would be easier to do this if you iterate over names and the data at the same time.

Try this with purrr :

library(purrr)

imap(my_list, function(x, y) imap(x, 
           ~write.table(.x, paste0(y, '_', .y , ".txt"),
            row.names = FALSE, quote = FALSE, sep = "\t", dec = ",")))

Or if you want to keep things in base R :

Map(function(x, y) {
  Map(function(p, q) {
    write.table(p, paste0(y, '_', q , ".txt"),
               row.names = FALSE, quote = FALSE, sep = "\t", dec = ",")
  }, x, names(x))
}, my_list, names(my_list))

You might want to unlist not recursively beforehand.

my_list.u <- unlist(my_list, recursive=F)
for (i in seq_along(my_list.u)) {
    filename=paste0("r_", names(my_list.u)[i], ".txt")
    write.table(my_list.u[[i]], filename, row.names=FALSE, quote=FALSE, 
                sep="\t", dec="," )
}

Or without for loop using sapply which is around 20% faster.

sapply(seq(my_list.u), function(x) 
  write.table(my_list.u[[x]], paste0(names(my_list.u)[x], ".txt"),
              row.names=FALSE, quote=FALSE, sep="\t", dec=","))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM