Exercise

Writing .csv and .tsv files

After importing and working with a dataset, you'll want to save it off to the disk to make sure that your hard work doesn't go to waste. In this exercise, you'll learn how.

If you've used write.csv() in the base package, you may have noticed that by default, row numbers are inserted as the leftmost column. Then when you import back into R, the row numbers show up as a column of data.

Not so with write_csv(), the equivalent function in the readr package. No duplicate row numbers are added. As with other functions you've seen, write_csv() has many optional arguments, but the required ones are x and path.

Take a look at the other arguments using ?write_csv. Notice especially that because the default value of col_names is !append, switching to append mode (add to the end of a pre-existing file) automatically disables the writing of column names, unless you manually override both. Makes sense, right?

Instructions

100 XP
  • Use write_csv() to save the cwts dataset as chickwts.csv.
  • Save cwts2, which has more chicken data, using the same function and file name, but this time set the append argument to TRUE.