---
author: "George G. Vega Yon"
---
# Getting R
To install R just follow the instructions available at http://cran.r-project.org
# Getting RStudio
RStudio is the most popular Integrated Development Environment (IDE) for R that is developed by the company of the same name. While having RStudio is not a requirement for using netdiffuseR, it is highly recommended.
To get RStudio just visit https://www.rstudio.com/products/rstudio/download/.
# Getting netdiffuseR
`netdiffuseR` has two different versions: the development version (available at https://github.com/USCCANA/netdiffuseR), and the stable version (available on https://cran.r-project.org/package=netdiffuseR). You can get either of the two but it is significantly easier to get the stable version (and we recommend to do so)
## CRAN version
For this version just go to your R console and type:
```r
install.packages("netdiffuseR")
```
## Github version
For the github version you will need to have installed the [`devtools`](https://cran.r-project.org/package=devtools) R package which allows to build netdiffuseR from source[^buildnetdiffuseR]. This can be done in the following steps:
[^buildnetdiffuseR]: Building an R package from source means that you will use `R CMD INSTALL` utility on the command line of your operating system. Depending on the R package, it may require having a C/C++ compiler such as gcc g++ or clang. This makes installing packages from source code a bit harder, that's why we recommend getting the CRAN version which is already compiled and ready for your operating system.
```r
install.packages("devtools") # If you don't have devtools already!
devtools::install_github("USCCANA/netdiffuseR")
```
# A ~~gentle~~ Quick n' Dirty Introduction to R
Some common tasks in R
0. 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:
```r
?read.csv
?"read.csv"
help(read.csv)
help("read.csv")
```
If you are not fully aware of what is the name of the function, you can always use the *fuzzy search*
```r
help.search("linear regression")
??"linear regression"
```
1. In R you can create new objects by either using the assign operator (`<-`) or the equal sign `=`, for example, the following 2 are equivalent:
```r
a <- 1
a = 1
```
Historically the assign operator is the most common used.
2. R has several type of objects, the most basic structures in R are `vectors`, `matrix`, `list`, `data.frame`. Here is an example creating several of these (each line is enclosed with parenthesis so that R prints the resulting element):
```{r creating}
(a_vector <- 1:9)
(another_vect <- c(1, 2, 3, 4, 5, 6, 7, 8, 9))
(a_string_vec <- c("I", "like", "netdiffuseR"))
(a_matrix <- matrix(a_vector, ncol = 3))
(a_string_mat <- matrix(letters[1:9], ncol=3)) # Matrices can be of strings too
(another_mat <- cbind(1:4, 11:14)) # The `cbind` operator does "column bind"
(another_mat2 <- rbind(1:4, 11:14)) # The `rbind` operator does "row bind"
(a_string_mat <- matrix(letters[1:9], ncol = 3))
(a_list <- list(a_vector, a_matrix))
(another_list <- list(my_vec = a_vector, my_mat = a_matrix)) # same but with names!
# Data frames can have multiple types of elements, it is a collection of lists
(a_data_frame <- data.frame(x = 1:10, y = letters[1:10]))
```
3. Depending on the type of object, we can access to its components using indexing:
```{r indexing}
a_vector[1:3] # First 3 elements
a_string_vec[3] # Third element
a_matrix[1:2, 1:2] # A sub matrix
a_matrix[,3] # Third column
a_matrix[3,] # Third row
a_string_mat[1:6] # First 6 elements of the matrix. R stores matrices by column.
# These three are equivalent
another_list[[1]]
another_list$my_vec
another_list[["my_vec"]]
# Data frames are just like lists
a_data_frame[[1]]
a_data_frame[,1]
a_data_frame[["x"]]
a_data_frame$x
```
4. Control-flow statements
```{r control-flow}
# The oldfashion forloop
for (i in 1:10) {
print(paste("I'm step", i, "/", 10))
}
# A nice ifelse
for (i in 1:10) {
if (i %% 2) # Modulus operand
print(paste("I'm step", i, "/", 10, "(and I'm odd)"))
else
print(paste("I'm step", i, "/", 10, "(and I'm even)"))
}
# A while
i <- 10
while (i > 0) {
print(paste("I'm step", i, "/", 10))
i <- i - 1
}
```
5. R has a very nice set of pseudo random number generation functions. In general, distribution functions have the following name structure:
a. Random Number Generation: `r[name-of-the-distribution]`, e.g. `rnorm` for normal, `runif` for uniform.
b. Density function: `d[name-of-the-distribution]`, e.g. `dnorm` for normal, `dunif` for uniform.
c. Cumulative Distribution Function (CDF): `p[name-of-the-distribution]`, e.g. `pnorm` for normal, `punif` for uniform.
d. Inverse (quantile) function: `q[name-of-the-distribution]`, e.g. `qnorm` for the normal, `qunif` for the uniform.
Here are some examples:
```{r random-numbers}
# To ensure reproducibility
set.seed(1231)
# 100,000 Unif(0,1) numbers
x <- runif(1e5)
hist(x)
# 100,000 N(0,1) numbers
x <- rnorm(1e5)
hist(x)
# 100,000 N(10,25) numbers
x <- rnorm(1e5, mean = 10, sd = 5)
hist(x)
# 100,000 Poisson(5) numbers
x <- rpois(1e5, lambda = 5)
hist(x)
# 100,000 rexp(5) numbers
x <- rexp(1e5, 5)
hist(x)
```
More distributions available at `??Distributions`.
For a nice intro to R, take a look at ["The Art of R Programming" by Norman Matloff](https://nostarch.com/artofr.htm). For more advanced users, take a look at ["Advanced R" by Hadley Wickham](http://adv-r.had.co.nz/).