1  Importing and Saving Data

Because the typical way of using R involves writing text into a file with the .r or .R extension (e.g. my-r-file.r). Hopefully you’re you are also doing this for the R commands you will use for this course. Sometimes you will also want to save your data.

1.1 Creating and saving an R script

You can create a new R file using the menu (File > New File > R script) or by using the keyboard combination . You are then prompted for a file name. When you want to save the file you can do so using the menu again (File > Save) or by pressing . Selecting ‘File > Save as …’ saves the active file under another name.

1.2 Saving and restoring your session

Save all objects to the working directory in a file called .RData:

save.image()

Or give the file a name:

save.image("mySession.RData") 

Save all commands entered into the R console during your session to the working directory in a file called .Rhistory:

savehistory()

Such a file may be hidden by default in some file browsers, including linux.

Note that this typically occurs (console dependent) by default when you quit your R session. If an .Rhistory file already exists from a previous session, the current session is appended to the end of this file and saved. Thus, by design, a complete log of your work is saved together with your script file, if your console session contains less than 512 lines.

Save specific objects:

save(object_1, object_2, object_3, file = "rObjects123.RData")

Restore or load previous sessions or objects:

load("mySession.RData")        # load from the working directory

When you save and load objects this way, loaded variables will have the same names as when they were stored. There is also a different way of saving objects where loaded objects are assigned explicitly.

myobject <- list(vec=c(1, 2, 3), mat=matrix(1:4,2))
saveRDS(myobject,file = 'myfile.rds')
object2 <- readRDS('myfile.rds')
identical(myobject, object2)
[1] TRUE

Often the data we want to use is not in any ‘R-format’ but in another format. We will then have to import this data. Much of this functionality is included in the foreign package. To open data from spss you can use the read.spss command from this package. The syntax is:

library(foreign)
read.spss(file = 'path/to/file/thespssfile.sav', to.data.frame = TRUE)

Using the argument to.data.frame we indicate that we want the result as a data.frame. Another format that is often used is excel sheets; for this we can use the read_excel function from the readxl package.

read_excel(path='path/to/file/excelworkbook.xls')

This function has an optional argument sheet to indicate the sheet in the excel file if it contains multiple sheets. Optionally, with range a cell range can be indicated.

Load .Rhistory file to access the history from the console:

loadhistory()