2.6 Importing and exporting data

The package rio enables us to import and export data sets from and into different formats (e.g., text, CSV, Excel, SPSS files). Traditionally, R users relied on functions such as read.table(), read.csv(), read_excel(), or read_sav() to import text files, csv comma-separated files, Excel files, or SPSS files. Similarly, the functions write.table(), write.csv(), write_xlsx(), and write_sav() were used to export data sets created and stored in the R session into an array of different formats. Unfortunately, to use most of these functions, R packages such as haven, readxl, or writexl were required.

Luckily, the package rio operates like a Swiss-army knife, allowing us to easily import and export data via generic functions whose arguments specify the type of file, the path, the name, etc.

2.6.1 Loading data

For example, we could read built-in data from the data sets loaded in our session using the function data(). From all the files available, we will read the data set women which includes the height and weight of 15 women.


data()
data(women)
women
##    height weight
## 1      58    115
## 2      59    117
## 3      60    120
## 4      61    123
## 5      62    126
## 6      63    129
## 7      64    132
## 8      65    135
## 9      66    139
## 10     67    142
## 11     68    146
## 12     69    150
## 13     70    154
## 14     71    159
## 15     72    164

Now, we will use the generic function import() from the package rio to read the burnout data set. The function head() shows the first six observations/participants of the R object included as the first argument of the function. In the following example, we are modifying the default settings that show the data of the first six observations/participants. We will request the first eight observations of the R object burnout.


my.burnout <- rio::import('./datasets/burnout.sav')
head(my.burnout, 8)
##   id sex age i1 i2 i3 i4 i5 i6 i7 i8 i9 i10 i11 i12 burnoutTot stress job.satis
## 1  1   0  34  4  3  4  2  3  4  3  4  4   4   4   4         34     88        48
## 2  2   0  38  2  2  4  3  4  2  3  2  3   3   4   3         28     79        53
## 3  3   1  43  2  5  5  2  4  2  2  5  2   3   3   1         22     82        68
## 4  4   1  37  3  3  2  4  4  3  3  2  3   4   3   3         33     89        54
## 5  5   0  43  2  2  4  3  3  1  3  4  3   2   4   2         23     76        51
## 6  6   1  35  3  3  4  3  3  3  3  3  3   3   4   2         29     83        47
## 7  7   1  31  2  4  1  4  4  4  4  2  4   2   5   3         37     86        59
## 8  8   1  25  3  4  1  4  3  2  4  2  4   5   3   4         38     84        50
##   coping.skills contract
## 1            71        2
## 2            82        2
## 3            56        3
## 4            64        1
## 5            73        3
## 6            74        3
## 7            75        1
## 8            80        2

CAUTION!

The data set that we want to import should be stored in the same folder of our working directory. If it is located in a different folder, the path should be specified in the code. For example, if the data set is in a folder named data inside of our working directory, the code should be as follows:

my.burnout <- rio::import('./data/burnout.sav')

IBM SPSS stores data frames into .sav files. Consequently, the file extension of the data set that we want to import needs to be explicitly included in the name of the file. To read CSV comma-delimited files, we will have to include the extension .csv. For Microsoft Excel files (i.e., spreadsheets) storing arrays of data sets using different sheets, we will use the file extension .xlsx.

Using two colons (::) to load specific functions

We can use functions from packages that we have not previously loaded by adding the name of the package followed by two colons before the specific function we intend to use [e.g., rio::import(), psych::desbribeBy()].

2.6.2 Exporting data

To export data from our R session, enter the R object (e.g., a data frame) as the first argument and the appropriate file extension as the second argument (e.g., format = 'txt'). Below, we will export the data set called my.burnout as an Excel file (i.e., format = 'xlsx').


rio::export(my.burnout, format = 'xlsx')