A.1 New to R
?
Now that your struggles with getting a grip on R
are fully acknowledged in advance, let’s try to avoid the ‘giving up’ from happening. Try to follow these steps to get started:
Get
R
and add some user comfort: Install the latestR
software and install a user interface like RStudio… It’s all free! An R interface will make some things easier, e.g., searching and installing packages from repositories. R Studio will also add functionality, like git/svn version control, project management and more, like the tools to create html pages like this one (knitr
andRmarkdown
). Another source of user comfort are thepackages
.R
comes with some basic packages installed, but you’ll soon need to fit generalised linear mixture models, or visualise social networks using graph theory and that means you’ll be searching for packages that allow you to do such things. A good place to start package hunting are the CRAN task view pages.Learn by running example
code
: Copy the commands in thecode
blocks you find on this page, or any other tutorial or help files (e.g., Rob Kabacoff’s Quick R). Paste them into an.R
script file in the script (or, source) editor. In R Studio You can run code by pressingcmd
+enter
when the cursor is on a single single line, or you can run multiple lines at once by selecting them first. If you get stuck remember that there are expertR
users who probably have answered your question already when it was posted on a forum. Search for example through the Stack overflow site for questions tagged withR
)Examine what happens… when you tell
R
to make something happen:R
stores variables (anything from numeric data to functions) in anEnvironment
. There are in fact many different environments, but we’ll focus on the main workspace for the currentR
session. If you run the commandx <- 1+1
, a variablex
will appear in theEnvironment
with the value2
assigned to it. Examining what happens in theEnvironment
is not the same as examining the output of a statistical analysis. Output inR
will appear in theConsole
window. Note that in a basic set-up each newR
session starts with an emptyEnvironment
. If you need data in another session, you can save the entireEnvironment
, or just some selected variables, to a file (.RData
).Learn about the properties of
R
objects: Think of objects as containers designed for specific content. One way to characterize the different objects inR
is by how picky they are about the content you can assign it. There are objects that holdcharacter
andnumeric
type data, amatrix
for numeric data organised in rows and columns, adata.frame
is a matrix that allows different data types in columns, and least picky of all is thelist
object. It can carry any other object, you can have alist
of which item 1 is an entiredata.frame
and item 2 is just acharacter
vector of the letterR
. The most difficult thing to master is how to efficiently work with these objects, how to assign values and query contents.Avoid repeating yourself: The
R
language has some amazing properties that allow execution of many repetitive algorithmic operations using just a few lines of code at speeds up to warp 10. Naturally, you’ll need to be at least half Vulcan to master these features properly and I catch myself copying code when I shouldn’t on a daily basis. The first thing you will struggle with are theapply
functions. These functions pass the contents of alist
object to a function. Suppose we need to calculate the means of column variables in 40 different SPSS.sav
files stored in the folderDAT
. With theforeign
package loaded we can execute the following commands:
data <- lapply(dir("/DAT/",pattern=".sav$"),read.spss)
out <- sapply(data,colMeans)
The first command applies read.spss to all files with a.sav
extension found in the folder/DAT
. It creates a data frame for each file which are all stored as elements of the listdata
. The second line applies the functioncolMeans
to each element ofdata
and puts the combined results in a matrix with dataset ID as columns (1-40), dataset variables as rows and the calculated column means as cells. This is just the beginning of theR
magic, wait ’till you learn how to write functions that can create functions.