Exercise

Writing .rds files

Sometimes, saving to a CSV file just won't cut it. If the R object you're working with has metadata associated with it, saving to a CSV will cause that information to be lost.

For example, if your dataset has one column of factors and other columns with characters, there will be no easy way to restore that metadata from a CSV. For this reason, readr offers a write_rds() function that exports an entire R object (metadata and all).

If you've used saveRDS() in the past, you will have no trouble using write_rds(). The only major difference between the two is that write_rds() does not compress the file by default.

The sister function of write_rds() is read_rds(). Again, this is very similar to base R's readRDS() (in fact, it's a wrapper around the same). It does, however, use a naming scheme that's consisent with the rest of readr.

Instructions

100 XP
  • Use write_rds() to save the trees data frame as trees.rds.
  • Use read_rds() to restore trees.rds to an R object called trees2.
  • Use identical() to determine whether trees is the same as trees2.