Created
August 5, 2021 00:47
-
-
Save jeremy-allen/4a1edafb388ab0370b36a294abb837f1 to your computer and use it in GitHub Desktop.
Create fake data and write 20,000 CSV file to disk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(data.table) # for fwrite() | |
library(stringr) | |
library(purrr) | |
# file names we will use when writing 20,000 CSV files to disk | |
filenames <- paste0("files/", "a_", | |
str_pad(1:20000, width = 5,side = "left", pad = "0"), | |
".csv") | |
# some vectors we can sample from to make a table | |
dates <- c(as.Date("2019-01-01"), as.Date("2020-01-01"), as.Date("2021-01-01")) | |
weights <- c(31:35) | |
colors <- c("red", "brown", "black") | |
pets <- c("cat", "dog", "bird") | |
# data frame of 5 columns and 1,000 rows | |
dat <- data.frame( | |
date = sample(dates, 1e3, replace = TRUE), | |
weight = sample(weights, 1e3, replace = TRUE), | |
pet = sample(pets, 1e3, replace = TRUE), | |
color = sample(colors, 1e3, replace = TRUE), | |
state = sample(state.abb, 1e3, replace = TRUE) | |
) | |
# write 20,000 CSV files to disk | |
walk(.x = filenames, .f = ~fwrite(x = dat, file = .x)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment