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
Rand add some user comfort: Install the latestRsoftware 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 (knitrandRmarkdown). Another source of user comfort are thepackages.Rcomes 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 thecodeblocks you find on this page, or any other tutorial or help files (e.g., Rob Kabacoff’s Quick R). Paste them into an.Rscript file in the script (or, source) editor. In R Studio You can run code by pressingcmd+enterwhen 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 expertRusers 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
Rto make something happen:Rstores 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 currentRsession. If you run the commandx <- 1+1, a variablexwill appear in theEnvironmentwith the value2assigned to it. Examining what happens in theEnvironmentis not the same as examining the output of a statistical analysis. Output inRwill appear in theConsolewindow. Note that in a basic set-up each newRsession 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
Robjects: Think of objects as containers designed for specific content. One way to characterize the different objects inRis by how picky they are about the content you can assign it. There are objects that holdcharacterandnumerictype data, amatrixfor numeric data organised in rows and columns, adata.frameis a matrix that allows different data types in columns, and least picky of all is thelistobject. It can carry any other object, you can have alistof which item 1 is an entiredata.frameand item 2 is just acharactervector 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
Rlanguage 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 theapplyfunctions. These functions pass the contents of alistobject to a function. Suppose we need to calculate the means of column variables in 40 different SPSS.savfiles stored in the folderDAT. With theforeignpackage 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.savextension 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 functioncolMeansto each element ofdataand 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 theRmagic, wait ’till you learn how to write functions that can create functions.