4.2 Write

write_delim, write_csv, write_tsv: write a tibble into a delimited file:

Table 4.2: write_delim and derived functions
name delim
write_delim needs to be set
write_csv ,: comma-separated
write_tsv : tab-separated
write_csv(x=f2,
          file="test_write_csv.csv")

# to get the exact same in base R, you would need to set a few additional parameters:
write.csv(x=f2,
          file="test_base_write_csv.csv", 
          quote=F, 
          row.names = F)

HANDS-ON

  • Read in file readr_example("mtcars.csv.zip") (also provided with the readr package)
  • With base R coding, remove rows where mpg > 20
  • Write this new tibble to a tab-separated file.
Answer
# Read in file readr_example("mtcars.csv.zip")
Cars <- read_csv(readr_example("mtcars.csv.zip"))

# With base R coding, remove rows where mpg > 20
mpg20 <- Cars[Cars$mpg > 20, ]

# Write this new tibble to a tab-separated file.
write_tsv(mpg20, "mtcars_mpg20.txt")