Save and load

Save and load a R object can be very useful. A common way to store your results in R is to create a txt, csv or xls file. The advantage is that you can open the tables with many programs on your computer. But as soon as you want to do something with it in R it becomes complicated. You need to import the csv file back into R. You need to define header, how the data is separated. And you can never be sure if a factor is still a factor or a date is still a date. Usually, there are some complications that need to be fixed.

An easier solution is to use the function “save” to store your R object. The advantage here is that all the properties of the object are retained. For example a data frame will be a data frame again, a list will be opened as a list.

Let’s create a data set.

# create a data set
petal.length <- rnorm(100, mean = 2.5, sd = 1)
petal.width <- rnorm(100, mean = 1.75, sd = 1)
species <- rep(letters[1:10], each = 10)
floral.traits <- data.frame(species, petal.length, petal.width)

# here we have specis as a factor and two numeric variables
str(floral.traits)
## 'data.frame':    100 obs. of  3 variables:
##  $ species     : Factor w/ 10 levels "a","b","c","d",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ petal.length: num  2.4 3.08 3.5 2.75 2.92 ...
##  $ petal.width : num  1.525 2.138 2.397 2.148 0.838 ...

Now let us do somthing with this data, for example calculate the mean petal length and petal width for each species.

# calculate mean petal length and withd for each species
mean.pl <- tapply(floral.traits$petal.length, floral.traits$species, mean)
mean.pw <- tapply(floral.traits$petal.width, floral.traits$species, mean)
# transpose the data to make the table easier to handle and combine the means to one data set.
means <- data.frame(t(rbind(mean.pl, mean.pw)))
means$species <- factor(rownames(means)) # add speceis column

We have calculated the mean petal length and width for each species. And now we want to store this result. Maybe we want to use this result later to plot or calculate something. But let’s save it for now with the function “save”.
The first argument is the name of the object you want to save or a list of objects. File is the name of the file with RData at the end. There are more options, but you will mostly use the two first arguments.

# save the R object
save(means, file = "MeanPetalLengthAndWidth.RData")
# let's remove the data set to make sure this is working.
rm(means)

Your R object is now stored in the working directory you are using. We also removed the object from the environment to make sure the object is saved and we can reload it again.

Now we want to load the object back again to our environment to use it further. For this we use the “load” function. The first argument here is the file name. With envir you define where the data should be loaded to. And with verbose you can decide if you want the item name to be printed or not. This can be useful if you have forgotten what it was called.

# load the R object
load("MeanPetalLengthAndWidth.RData", verbose = TRUE)
## Loading objects:
##   means
# check the properties of the object.
str(means)
## 'data.frame':    10 obs. of  3 variables:
##  $ mean.pl: num  2.56 2.34 2.68 2.32 3.23 ...
##  $ mean.pw: num  1.82 1.76 1.75 1.74 2.34 ...
##  $ species: Factor w/ 10 levels "a","b","c","d",..: 1 2 3 4 5 6 7 8 9 10

Now you can check if the object has reapeard in the environment. And then you can . Have fun saving and loading!

# Barplot of petal length
barplot(means$mean.pl, names.arg = means$species, ylab = "Petal length", xlab = "Species")

plot of chunk unnamed-chunk-5