How do I write multiple CSV files from a list of data frames?

前端 未结 2 1483
一生所求
一生所求 2021-02-04 19:28

I have a list with two data frames. I want to loop thru the list and write a CSV for each data frame and name it after the data frame name.

library(ggplot2)
myLi         


        
2条回答
  •  孤独总比滥情好
    2021-02-04 20:05

    A few of things.

    1. myList doesn't have any names, so you aren't actually naming your files.
    2. Once you give myList names, you would do better to index along the names than along the list
    3. You use df in your for loop, but that isn't defined anywhere.

    This should work (untested)

    myList <- list(diamonds = diamonds, 
                   cars = cars)
    for(i in names(mylist)){
      write.csv(myList[[i]], paste0(i,".csv"))
    }
    

提交回复
热议问题