循环写入多个CSV文件 [英] Write Multiple CSV files in a loop
问题描述
我有一个具有150万行的csv文件,该文件由2列名称和电子邮件组成.我想编写一个程序,以便当我在R中读取文件时,每个csv中的输出被分割成5000个数据.
I have a csv file with 1.5 million rows which consists of 2 columns name and email.I want to write a program in such way that when I read my file in R, the output is segmented of 5000 data in each csv.
也许我可以循环执行此操作:从第1行运行到5000,然后将其保存为project1.csv,然后将其保存为project1.csv,然后将其保存到project2.csv,然后在工作目录的project3.csv中保存为10001至15000.有什么建议吗?
Maybe I can do this with a loop: run from row 1 to 5000 and save it as project1.csv and then 5001 to 10000 and save to project2.csv and then 10001 till 15000 in project3.csv in my working directory. Any suggestions?
推荐答案
假设'df1'是data.frame
,我们需要每隔5000行进行细分并将其保存在新文件中,我们将split
数据集根据到list
(lst
)的行序列创建分组索引.我们遍历list
元素(lapply(...
)的顺序,并使用write.csv
写入新文件.
Assuming that 'df1' is the data.frame
which we need to segment every 5000 rows and save it in a new file, we split
the dataset by creating a grouping index based on the sequence of rows to a list
(lst
). We loop through the sequence of list
elements (lapply(...
), and write new file with write.csv
.
n <- 5000
lst <- split(df1, ((seq_len(nrow(df1)))-1)%/%n+1L)
invisible(lapply(seq_along(lst), function(i)
write.csv(lst[[i]], file=paste0('project', i, '.csv'), row.names=FALSE)))
这篇关于循环写入多个CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!