Write .csv in a for loop

家住魔仙堡 提交于 2019-12-20 04:34:53

问题


I want to write csv files in a for loop. Say I have a data frame data with 3 rows, to make it simple, of a variable x. In the end, I want my output to be 200 .csv files, each one containing a row of the data. The first column of the data is the identification ("ID") of my variables.

Furthermore, my data is described as the following:

  data:

     ID x
 [1] a  1
 [2] b  2
 [3] c  3 

 for (i in nrow(data)){
   write.csv(data[i,2], file = paste0("Directory/", "data[i,1], ".csv"))
 }

I run this code and a csv file is created. However, only the last row is created, meaning that I'm found with only one file c.csv.

Do you know what I am doing wrong? I thought that it would automatically create all the archives. Should I save first the results in a list and then export them?


回答1:


No need to use a loop. You can use a data.table approach, which will be more efficient and faster.

library(data.table)

# create a column with row positions
setDT(dt)[, rowpos := .I]

# save each line of your dataset into a separate .csv file
dt[, write.csv(.SD, paste0("output_", rowpos,".csv")), 
                  by = rowpos, .SDcols=names(dt) ]

Making things much faster

# Now in case you're working with a large dataset and you want
# to make things much faster, you can use `fwrite {data.table}`*

dt[, fwrite(.SD, paste0("output_", rowpos ,".csv")), 
               by = rowpos, .SDcols=names(dt) ]

Using a Loop

# in case you still want to use a loop, this will do the work for you:

for (i in 1:nrow(dt)){
                      write.csv(dt[i,], file = paste0("loop_", i, ".csv"))
                      }

Extra: Saving subsets of dataframe by groups instead of by rows

# This line of code will save a separate `.csv` file for every ID 
# and name the file according to the ID


 setDT(dt)[, fwrite(.SD, paste0("output_", ID,".csv")), 
                       by = ID, .SDcols=names(dt) ]

* ps. note that fwriteis still in the development version of data.table 1.9.7. Go here for install instructions.



来源:https://stackoverflow.com/questions/37556184/write-csv-in-a-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!