To export data to the hard drive, you need the file path and an extension. First of all, the path is the location where the data will be stored. In this tutorial, you will see how to store data on the hard drive. Secondly, R allows the users to export the data into different types of files. We cover the essential file’s extension:

  • csv
  • xlsx
  • RDS
  • SAS
  • SPSS
  • STATA

Overall, it is not difficult to export data from R.

Export to Hard drive

To begin with, you can save the data directly into the working directory. The following code prints the path of your working directory:

directory <- getwd()
directory
## [1] "/Volumes/GoogleDrive/Il mio Drive/An-R-Tutorial-for-Beginners"

By default, the file will be saved in the below path.

For Mac OS:

/Users/USERNAME

For Windows:

C:\Users\USERNAME

You can, of course, set a different path. For instance, you can change the path to the download folder.

Create data frame

First of all, let’s import the mtcars dataset and get the mean of mpg and disp grouped by gear.

library(dplyr)
df <- mtcars %>%
    select(mpg, disp, gear) %>%
    group_by(gear) %>%
    summarise(mean_mpg = mean(mpg), mean_disp = mean(disp))
df
gear mean_mpg mean_disp
3 16.10667 326.3000
4 24.53333 123.0167
5 21.38000 202.4800

The table contains three rows and three columns. You can create a CSV file with the function write.csv().

Export CSV

The basic syntax is:

write.csv(df, path)

Arguments

  • df: Dataset to save. Need to be the same name of the data frame in the environment.
  • path: A string. Set the destination path. Path + filename + extension i.e. “/Users/USERNAME/Downloads/mydata.csv” or the filename + extension if the folder is the same as the working directory

Example:

write.csv(df, "table_car.csv")

Code Explanation

  • write.csv(df, "table_car.csv"): Create a CSV file in the hard drive:
    • df: name of the data frame in the environment
    • “table_car.csv”: Name the file table_car and store it as csv

Note: You can use the function write.csv2() to separate the rows with a semicolon.

write.csv2(df, "table_car.csv")

Note: For pedagogical purpose only, we created a function called open_folder() to open the directory folder for you. You just need to run the code below and see where the csv file is stored. You should see a file names table_car.csv.

# Run this code to create the function
open_folder <-function(dir){
    if (.Platform['OS.type'] == "windows"){
      shell.exec(dir)  
    } else {
      system(paste(Sys.getenv("R_BROWSER"), dir))
  }
}
# Call the function to open the folder
open_folder(directory)

Export to Excel file

Export data to Excel is trivial for Windows users and trickier for Mac OS user. Both users will use the library xlsx to create an Excel file. The slight difference comes from the installation of the library. Indeed, the library xlsx uses Java to create the file. Java needs to be installed if not present in your machine.

You can install the library directly with

install.packages("xlsx")

Once the library installed, you can use the function write.xlsx(). A new Excel workbook is created in the working directory

library(xlsx)
write.xlsx(df, "table_car.xlsx")

Export to different software

Exporting data to different software is as simple as importing them. The library “haven” provides a convenient way to export data to

  • spss
  • sas
  • stata

First of all, import the library. If you don’t have “haven”, you can install it install.packages(haven).

library(haven)  

SPSS file

Below is the code to export the data to SPSS software:

write_sav(df, "table_car.sav")  

Export SAS file

Just as simple as spss, you can export to sas

write_sas(df, "table_car.sas7bdat")

Export STATA file

Finally, haven library allows writing .dta file.

write_dta(df, "table_car.dta")

R

If you want to save a data frame or any other R object, you can use the save() function.

save(df, file ='table_car.RData')

You can check the files created above in the present working directory.

 

A work by Gianluca Sottile

gianluca.sottile@unipa.it