Write

Although there are specific functions for some delimiters this page will show you how to write out data with any delimiter using readr::write_delim() and the delim= option.

The most common types of delimiters for rectangular data files are:

Additionally, non-standard delimiters can be specified. Tilde (~), Colon (:), Semi-colon (;), Pipe (|) are commonly used as non-standard delimiters.

Data

We’ll write the first five lines of all_plant_details.csv to various files with different delimiters.

For convenience read in the data, slice it and assign it to a variable called plant_detail_slice_tbl.

plant_detail_slice_tbl <- readr::read_delim(
    file = "https://neof-workshops.github.io/Tidyverse/data/all_plant_details.csv",
    delim = ",", show_col_types = FALSE) |>
    dplyr::slice(1:5) 

Comma delimited

To write a data to a file with comma (,) delimiters you can use the option delim = ",".

Write comma delimited file

Write the variable plant_detail_slice_tbl to a comma delimited file called all_plant_slice.csv.

readr::write_delim(plant_detail_slice_tbl, "plant_detail_slice.csv", delim = ",")

Comma delimited file contents

Print the file contents of all_plant_slice.txt.

readLines("plant_detail_slice.csv")
[1] "id,common_name,seeds,drought_tolerant,salt_tolerant,thorny,invasive,tropical,indoor,flowers,cones,fruits,edible_fruit,leaf,edible_leaf,cuisine,medicinal,poisonous_to_humans,poisonous_to_pets,sunlight_part_sun_part_shade,sunlight_full_shade,sunlight_deep_shade,sunlight_part_shade,sunlight_full_sun_only_if_soil_kept_moist,sunlight_full_sun,sunlight_filtered_shade,care_level_encoded,maintenance_encoded,watering_encoded,growth_rate_encoded,cycle_perennial,cycle_herbaceous_perennial,cycle_annual"
[2] "425,flowering-maple,0,1,0,1,0,1,1,1,0,0,0,1,0,0,1,0,0,0,0,0,1,0,1,0,2,0,2,0,1,0,0"                                                                                                                                                                                                                                                                                                                                                                                                                              
[3] "426,flowering-maple,0,1,0,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,0,0"                                                                                                                                                                                                                                                                                                                                                                                                                              
[4] "427,flowering-maple,0,1,0,0,0,1,1,1,0,0,0,1,0,0,1,0,0,0,0,0,1,0,1,0,1,0,1,0,1,0,0"                                                                                                                                                                                                                                                                                                                                                                                                                              
[5] "428,flowering-maple,0,1,1,0,0,1,1,1,0,0,0,1,0,0,1,0,0,0,0,0,0,0,1,0,2,1,1,0,1,0,0"                                                                                                                                                                                                                                                                                                                                                                                                                              
[6] "434,Jacob's coat,0,0,0,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0,1,0,1,0,2,0,2,0,1,0,0"                                                                                                                                                                                                                                                                                                                                                                                                                                 

Tab delimited

To write a data to a file with comma (\t) delimiters you can use the option delim = "\t".

Write tab delimited file

Write the variable plant_detail_slice_tbl to a tab delimited file called all_plant_slice.tsv.

readr::write_delim(plant_detail_slice_tbl, "plant_detail_slice.tsv", delim = "\t")

Tab delimited file contents

Print the file contents of all_plant_slice.tsv.

If you were to open the file in a text editor it would most likely represent the \t characters as tab space.

readLines("plant_detail_slice.tsv")
[1] "id\tcommon_name\tseeds\tdrought_tolerant\tsalt_tolerant\tthorny\tinvasive\ttropical\tindoor\tflowers\tcones\tfruits\tedible_fruit\tleaf\tedible_leaf\tcuisine\tmedicinal\tpoisonous_to_humans\tpoisonous_to_pets\tsunlight_part_sun_part_shade\tsunlight_full_shade\tsunlight_deep_shade\tsunlight_part_shade\tsunlight_full_sun_only_if_soil_kept_moist\tsunlight_full_sun\tsunlight_filtered_shade\tcare_level_encoded\tmaintenance_encoded\twatering_encoded\tgrowth_rate_encoded\tcycle_perennial\tcycle_herbaceous_perennial\tcycle_annual"
[2] "425\tflowering-maple\t0\t1\t0\t1\t0\t1\t1\t1\t0\t0\t0\t1\t0\t0\t1\t0\t0\t0\t0\t0\t1\t0\t1\t0\t2\t0\t2\t0\t1\t0\t0"                                                                                                                                                                                                                                                                                                                                                                                                                              
[3] "426\tflowering-maple\t0\t1\t0\t0\t0\t0\t1\t1\t0\t0\t0\t1\t0\t0\t0\t0\t0\t0\t0\t0\t1\t0\t1\t0\t1\t0\t1\t0\t1\t0\t0"                                                                                                                                                                                                                                                                                                                                                                                                                              
[4] "427\tflowering-maple\t0\t1\t0\t0\t0\t1\t1\t1\t0\t0\t0\t1\t0\t0\t1\t0\t0\t0\t0\t0\t1\t0\t1\t0\t1\t0\t1\t0\t1\t0\t0"                                                                                                                                                                                                                                                                                                                                                                                                                              
[5] "428\tflowering-maple\t0\t1\t1\t0\t0\t1\t1\t1\t0\t0\t0\t1\t0\t0\t1\t0\t0\t0\t0\t0\t0\t0\t1\t0\t2\t1\t1\t0\t1\t0\t0"                                                                                                                                                                                                                                                                                                                                                                                                                              
[6] "434\tJacob's coat\t0\t0\t0\t0\t0\t0\t1\t1\t0\t0\t0\t1\t0\t0\t0\t0\t0\t0\t0\t0\t1\t0\t1\t0\t2\t0\t2\t0\t1\t0\t0"                                                                                                                                                                                                                                                                                                                                                                                                                                 

Space delimited

By default readr::write_delim() will use a space as the delimiter character. Therefore you do not need to provide the delim= option.

Write space delimited file

Write the variable plant_detail_slice_tbl to a space delimited file called all_plant_slice.txt.

readr::write_delim(plant_detail_slice_tbl, "plant_detail_slice.txt")

Space delimited file contents

Print the file contents of all_plant_slice.txt.

You will notice the 6th line has "Jacob’s coat". As this value had a space the \" are used to indicate that Jacob’s coat is the value within one field.

readLines("plant_detail_slice.txt")
[1] "id common_name seeds drought_tolerant salt_tolerant thorny invasive tropical indoor flowers cones fruits edible_fruit leaf edible_leaf cuisine medicinal poisonous_to_humans poisonous_to_pets sunlight_part_sun_part_shade sunlight_full_shade sunlight_deep_shade sunlight_part_shade sunlight_full_sun_only_if_soil_kept_moist sunlight_full_sun sunlight_filtered_shade care_level_encoded maintenance_encoded watering_encoded growth_rate_encoded cycle_perennial cycle_herbaceous_perennial cycle_annual"
[2] "425 flowering-maple 0 1 0 1 0 1 1 1 0 0 0 1 0 0 1 0 0 0 0 0 1 0 1 0 2 0 2 0 1 0 0"                                                                                                                                                                                                                                                                                                                                                                                                                              
[3] "426 flowering-maple 0 1 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 0"                                                                                                                                                                                                                                                                                                                                                                                                                              
[4] "427 flowering-maple 0 1 0 0 0 1 1 1 0 0 0 1 0 0 1 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 0"                                                                                                                                                                                                                                                                                                                                                                                                                              
[5] "428 flowering-maple 0 1 1 0 0 1 1 1 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1 0 2 1 1 0 1 0 0"                                                                                                                                                                                                                                                                                                                                                                                                                              
[6] "434 \"Jacob's coat\" 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 1 0 1 0 2 0 2 0 1 0 0"                                                                                                                                                                                                                                                                                                                                                                                                                             

Non-standard delimiters

You can use many other characters as delimiters when writing files. This can be useful if your data contains the three common delimiters (comm, tab, and space).

The most common non-standard delimiters are:

  • Tilde (~)
  • Colon (:)
  • Semi-colon (;)
  • Pipe (|)

Write non-standard delimited file

Write the variable plant_detail_slice_tbl to a pipe (|) delimited file called all_plant_slice.pipe_delimit.text.

readr::write_delim(plant_detail_slice_tbl, "plant_detail_slice.pipe_delimit.text", delim = "|")

Non-standard delimited file contents

Print the file contents of all_plant_slice.pipe_delimit.text.

readLines("plant_detail_slice.pipe_delimit.text")
[1] "id|common_name|seeds|drought_tolerant|salt_tolerant|thorny|invasive|tropical|indoor|flowers|cones|fruits|edible_fruit|leaf|edible_leaf|cuisine|medicinal|poisonous_to_humans|poisonous_to_pets|sunlight_part_sun_part_shade|sunlight_full_shade|sunlight_deep_shade|sunlight_part_shade|sunlight_full_sun_only_if_soil_kept_moist|sunlight_full_sun|sunlight_filtered_shade|care_level_encoded|maintenance_encoded|watering_encoded|growth_rate_encoded|cycle_perennial|cycle_herbaceous_perennial|cycle_annual"
[2] "425|flowering-maple|0|1|0|1|0|1|1|1|0|0|0|1|0|0|1|0|0|0|0|0|1|0|1|0|2|0|2|0|1|0|0"                                                                                                                                                                                                                                                                                                                                                                                                                              
[3] "426|flowering-maple|0|1|0|0|0|0|1|1|0|0|0|1|0|0|0|0|0|0|0|0|1|0|1|0|1|0|1|0|1|0|0"                                                                                                                                                                                                                                                                                                                                                                                                                              
[4] "427|flowering-maple|0|1|0|0|0|1|1|1|0|0|0|1|0|0|1|0|0|0|0|0|1|0|1|0|1|0|1|0|1|0|0"                                                                                                                                                                                                                                                                                                                                                                                                                              
[5] "428|flowering-maple|0|1|1|0|0|1|1|1|0|0|0|1|0|0|1|0|0|0|0|0|0|0|1|0|2|1|1|0|1|0|0"                                                                                                                                                                                                                                                                                                                                                                                                                              
[6] "434|Jacob's coat|0|0|0|0|0|0|1|1|0|0|0|1|0|0|0|0|0|0|0|0|1|0|1|0|2|0|2|0|1|0|0"