Appendix A — readr example data

This document creates some toy data to be read in by readr as shown in readr notes.

my_vctr <- function(x) {
  sample(x, 50, replace = TRUE)
}
my_dates <- function(x) {
  formatC(my_vctr(x), width = 2, flag = "0")
}
years <- 1200:1700
months <- 1:12


set.seed(240)
df <- data.frame(
  a = my_vctr(letters),
  b = my_vctr(c("", "NA", letters[1:5])),
  c = runif(50, 10, 100), 
  d = my_vctr(c("T", "F")),
  e = my_vctr(c("hello", "goodbye", "kook", "gnarly", "none")), 
  f = my_vctr(20:50),
  g = paste0("$", formatC(runif(50, 1000, 200000), digits = 2, big.mark = ",", format = "f")),
  h = paste0(my_vctr(years), my_dates(months), my_dates(1:28)),
  i = paste(my_vctr(years), my_dates(months), my_dates(1:28), sep = "-"),
  j = paste(my_vctr(1:28), my_vctr(month.name), my_vctr(years)),
  k = paste(my_vctr(month.abb), my_vctr(1:28), my_vctr(years)),
  l = my_vctr(10000:500000),
  m = paste0("2023", "-", my_dates(months), "-", my_dates(1:28), " ", my_dates(0:23), ":", my_dates(0:59))
)

head(df)
  a b        c d      e  f           g        h          i               j
1 p b 95.15645 T gnarly 48  $42,498.74 14651220 1585-05-06 20 January 1503
2 x b 71.06688 F   none 36 $141,971.80 15820913 1366-12-10   23 April 1390
3 e d 15.56805 F   kook 35  $15,852.71 15460815 1606-06-05     4 July 1585
4 o b 57.60559 T   kook 46  $66,555.38 14631127 1675-01-14 20 October 1237
5 v d 91.12470 T  hello 23 $143,747.96 13520326 1400-05-12    27 July 1394
6 r d 40.04119 T gnarly 36  $46,013.41 13350525 1578-09-14    26 July 1593
            k      l                m
1  Feb 4 1313 150248 2023-05-16 02:57
2 Jun 17 1553 197379 2023-04-28 23:26
3 Sep 18 1299 101224 2023-09-22 13:17
4  Dec 1 1558 225648 2023-02-27 07:48
5 Oct 21 1297 300957 2023-02-11 17:22
6 Nov 23 1621  43411 2023-06-02 06:30

Column type specifications

  • a: character
  • b: character with NAs as blank character and “NA”
  • c: double
  • d: logical
  • e: factor with optional NA with “none”
  • f: integer
  • g: number
  • h: date: “20230316”
  • i: date in locale: “2023-03-16”
  • j: date: 16 March 2023
  • k: date: Mar 16 2023
  • l: time
  • m: datetime in locale: “2023-03-16 11:38”

Write the data frame to data/

write.csv(df, "data/readr-example.csv", row.names = FALSE)