r - Write Multiple CSV files in a loop -
i have csv file 1.5 million rows consists of 2 columns name , email.i want write program in such way when read file in r, output segmented of 5000 data in each csv.
maybe can loop: run row 1 5000 , save project1.csv , 5001 10000 , save project2.csv , 10001 till 15000 in project3.csv in working directory. suggestions?
assuming 'df1' data.frame
need segment every 5000 rows , save in new file, split
dataset creating grouping index based on sequence of rows list
(lst
). loop through sequence of list
elements (lapply(...
), , write new file 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)))
Comments
Post a Comment