Exporting Data

Learning Goals

At the end of this lesson, you should:

  • Be able to export tabular data from R into a text file, .csv or .xlsx file
  • Be able to save an R object and reload it into an R session.

This lesson is focused on exporting tabular data. It is a very short lesson because exporting data is quite similar to importing data. Like in data import, exporting data involves opening a connection between R and file system, writing the data to file and closing the connection.

First, we need to load some data to write to file. R packages often come with data sets that can loaded with the data() command.

data("mtcars")

Exporting to CSV files

write.csv()

First, check the documentation: ?write.csv

write.csv(mtcars, here::here("outputs", "mtcars_1.csv"), row.names = FALSE)

Setting the row.names argument to FALSE ensures that a column of numbers without a header is not included in the file (which is likely to cause import errors in the future).

write.table()

This function looks very similar to write.csv() because it is technically the same function. write.csv() is wrapper for write.table() using a specific set of default arguments for CSV files (e.g. sep = ","). In this case, we cannot rely on those default and must specify

write.table(mtcars, here::here("outputs", "mtcars_2.csv"), 
          row.names = FALSE, sep = ",", quote = FALSE)

write_csv()

This function is very similar to read.csv(), but it does not have a row names argument because it does not output rownames.

library(readr)

write_csv(mtcars, here::here("outputs", "mtcars_3.csv"))

write_csv() is wrapper for write_delim().

Exporting to text file

This also uses write.table() or write.table():

write.table(mtcars, here::here("outputs", "mtcars.txt"), sep = "\t",
            quote = "none")

write_delim(mtcars, here::here("outputs", "mtcars.txt"), delim = "\t",
            quote = "none")

Exporting to Excel file

Use the writexl package:

library(writexl)

write_xlsx(mtcars, here::here("outputs", "mtcars.xlsx"))

The help file is informative: ?write_xlsx

Other options

save()

This is special option to save objects in your environment to file. These can only be used by R, but are convenient if you plan to return to these object:

Save one object:

save(mtcars, file = here::here("outputs", "mydata.RData"))

Save multiple objects:

data("sleep")
save(mtcars, sleep, file = here::here("outputs", "more.RData"))

These can be loaded back into an R session as thus:

load(here::here("outputs", "mydata.RData"))
Note

If you are working with a specialized file type that has dedicated libraries for importing them into R and manipulating those objects, those dedicated libraries likely have export functions for that file type. For spatial object, the package sf can import, alter, and export shapefiles.

  1. Import one of your data sets using two of the functions taught:

(save your data in different format to enable this)

  • read.csv()
  • read_csv()
  • read_excel()
  • read.delim()
  1. Examine the data imported using View(imported_data). Did everything import as expected? Are your variables coded as they should be? Are numeric variables numeric? Are missing data detected as thus? Where any row names accidentally introduced? Were missing cells converted to “NA”? Did any data become unexpectedly quoted?
Putting it all together

Exporting data is a good thing to do during the data wrangling process. Once you have arranged in your data set into an ideal state, save it so you an easily reload it later.

It is very important that you check your output file (especially as a newer R programmer!) to make sure everything is as you expect. We have all accidentally output “myfile” instead of “myfile.csv”, and this can be highly inconvenient! You can do by manually opening the file or by importing back into R; either should tell you if the file export went as expected or not.