You will frequently need to both import and export data as you work. Fortunately R makes this quite easy. We will do this a bit backward just so we can keep using the handy sample data sets R provides. Typically you would do this with your own data.

We’ll use the write.csv() function. As always its a good idea to read the documentation first help("write.csv").


write.csv(state.x77, file="my_state_x77.csv")


If you look in the Files tab in your lower right pane you should see this file created now.

Now lets read it in using the complimentary function read.csv().


my_x77 <- read.csv("my_state_x77.csv")


Try It Out

CSV (Comma Separated Value) files are often preferred. Compared to Excel or other spreadsheet documents, they can be read by anything, and they never become unreadable like older Excel documents become. However, you will often get Excel data. Research methods to import this data to.