R (R Core Team 2024) is a programming language oriented to statistical computing. R has become the de facto programming language in the social network community due to the large number of packages available for network analysis. R packages are collections of functions, data, and documentation that extend R. A good reference book for both novice and advanced users is “The Art of R programming”Matloff (2011)1.
Getting R
You can get R from the Comprehensive R Archive Network website [CRAN] (link). CRAN is a network of servers worldwide that store identical, up-to-date versions of code and documentation for R. CRAN website also has a lot of information about R, including manuals, FAQs, and mailing lists.
Although R comes with a Graphical User Interface [GUI], I recommend getting an alternative like RStudio or VSCode. RStudio and VSCode are excellent companions for programming in R. While RStudio is more common among R users, VSCode is a more general-purpose IDE that can be used for many other programming languages, including Python and C++.
How to install packages
Nowadays, there are two ways of installing R packages (that I’m aware of), either using install.packages, which is a function shipped with R, or using the devtools R package to install a package from some remote repository other than CRAN, here are a few examples:
# This will install the igraph package from CRAN>install.packages("netdiffuseR")# This will install the bleeding-edge version from the project's GitHub repo!> devtools::install_github("USCCANA/netdiffuseR")
The first one, using install.packages, installs the CRAN version of netdiffuseR, whereas the line of code installs whatever version is published on https://github.com/USCCANA/netdiffuseR, which is usually called the development version.
In some cases, users may want/need to install packages from the command line as some packages need extra configuration to be installed. But we won’t need to look at it now.
A gentle Quick n’ Dirty Introduction to R
Some common tasks in R
Getting help (and reading the manual) is THE MOST IMPORTANT thing you should know about. For example, if you want to read the manual (help file) of the read.csv function, you can type either of these:
In R, you can create new objects by either using the assign operator (<-) or the equal sign =, for example, the following two are equivalent: r a <- 1 a = 1 Historically, the assign operator is the most commonly used.
R has several types of objects. The most basic structures in R are vectors, matrix, list, data.frame. Here is an example of creating several of these (each line is enclosed with parenthesis so that R prints the resulting element):
(a_vector <-1:9)
[1] 1 2 3 4 5 6 7 8 9
(another_vect <-c(1, 2, 3, 4, 5, 6, 7, 8, 9))
[1] 1 2 3 4 5 6 7 8 9
(a_string_vec <-c("I", "like", "netdiffuseR"))
[1] "I" "like" "netdiffuseR"
(a_matrix <-matrix(a_vector, ncol =3))
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
# Matrices can be of strings too(a_string_mat <-matrix(letters[1:9], ncol=3))
Matloff, Norman. 2011. The Art of r Programming: A Tour of Statistical Software Design. No Starch Press.
R Core Team. 2024. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.
Here a free pdf version distributed by the author.↩︎