shiny
shiny

Reputation: 3502

Split and save dataframe into several csv files based on the number of rows

I have a dataframe that has 1000 columns and 24729 rows. I need to save it as several csv files (for example 10) split based on the number of rows.

I have checked many questions and most of them are for data.frames that can be split using levels of a factor. I could not find any examples that can be done based on number of rows.

I did a number of trials but none of them worked.

How would you save diamonds as 10 csv files split based on the number of rows?

library(ggplot)
df = diamonds

Upvotes: 1

Views: 740

Answers (1)

r.user.05apr
r.user.05apr

Reputation: 5456

The following writes 10 csv's to your working directory:

library(tidyverse)

df = diamonds

SPLITS <- 10
tmp <- sort(rep(seq_len(SPLITS), length.out = nrow(df)))
iwalk(split(df, tmp), ~write_csv(.x, str_c(str_c(getwd(), "/", .y, ".CSV"))))

Upvotes: 2

Related Questions