Skip to content

Commit 08a659b

Browse files
committed
Forgot to add the quarto docs
1 parent b9d026b commit 08a659b

7 files changed

Lines changed: 1760 additions & 0 deletions

File tree

0-getstarted.qmd

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
---
2+
author: "George G. Vega Yon, Ph.D."
3+
---
4+
5+
# Getting R
6+
7+
To install R just follow the instructions available at http://cran.r-project.org
8+
9+
# Getting RStudio
10+
11+
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.
12+
13+
To get RStudio just visit https://www.rstudio.com/products/rstudio/download/.
14+
15+
# Getting netdiffuseR
16+
17+
`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)
18+
19+
## CRAN version
20+
21+
For this version just go to your R console and type:
22+
23+
```r
24+
install.packages("netdiffuseR")
25+
```
26+
27+
## Github version
28+
29+
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:
30+
31+
[^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.
32+
33+
```r
34+
install.packages("devtools") # If you don't have devtools already!
35+
devtools::install_github("USCCANA/netdiffuseR")
36+
```
37+
38+
# A ~~gentle~~ Quick n' Dirty Introduction to R
39+
40+
Some common tasks in R
41+
42+
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:
43+
```r
44+
?read.csv
45+
?"read.csv"
46+
help(read.csv)
47+
help("read.csv")
48+
```
49+
If you are not fully aware of what is the name of the function, you can always use the *fuzzy search*
50+
```r
51+
help.search("linear regression")
52+
??"linear regression"
53+
```
54+
55+
56+
57+
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:
58+
```r
59+
a <- 1
60+
a = 1
61+
```
62+
Historically the assign operator is the most common used.
63+
64+
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):
65+
```{r creating}
66+
(a_vector <- 1:9)
67+
(another_vect <- c(1, 2, 3, 4, 5, 6, 7, 8, 9))
68+
(a_string_vec <- c("I", "like", "netdiffuseR"))
69+
(a_matrix <- matrix(a_vector, ncol = 3))
70+
(a_string_mat <- matrix(letters[1:9], ncol=3)) # Matrices can be of strings too
71+
(another_mat <- cbind(1:4, 11:14)) # The `cbind` operator does "column bind"
72+
(another_mat2 <- rbind(1:4, 11:14)) # The `rbind` operator does "row bind"
73+
(a_string_mat <- matrix(letters[1:9], ncol = 3))
74+
(a_list <- list(a_vector, a_matrix))
75+
(another_list <- list(my_vec = a_vector, my_mat = a_matrix)) # same but with names!
76+
# Data frames can have multiple types of elements, it is a collection of lists
77+
(a_data_frame <- data.frame(x = 1:10, y = letters[1:10]))
78+
```
79+
80+
3. Depending on the type of object, we can access to its components using indexing:
81+
```{r indexing}
82+
a_vector[1:3] # First 3 elements
83+
a_string_vec[3] # Third element
84+
a_matrix[1:2, 1:2] # A sub matrix
85+
a_matrix[,3] # Third column
86+
a_matrix[3,] # Third row
87+
a_string_mat[1:6] # First 6 elements of the matrix. R stores matrices by column.
88+
89+
# These three are equivalent
90+
another_list[[1]]
91+
another_list$my_vec
92+
another_list[["my_vec"]]
93+
94+
# Data frames are just like lists
95+
a_data_frame[[1]]
96+
a_data_frame[,1]
97+
a_data_frame[["x"]]
98+
a_data_frame$x
99+
```
100+
101+
4. Control-flow statements
102+
```{r control-flow}
103+
# The oldfashion forloop
104+
for (i in 1:10) {
105+
print(paste("I'm step", i, "/", 10))
106+
}
107+
108+
# A nice ifelse
109+
110+
for (i in 1:10) {
111+
112+
if (i %% 2) # Modulus operand
113+
print(paste("I'm step", i, "/", 10, "(and I'm odd)"))
114+
else
115+
print(paste("I'm step", i, "/", 10, "(and I'm even)"))
116+
117+
}
118+
119+
# A while
120+
i <- 10
121+
while (i > 0) {
122+
print(paste("I'm step", i, "/", 10))
123+
i <- i - 1
124+
}
125+
```
126+
127+
5. R has a very nice set of pseudo random number generation functions. In general, distribution functions have the following name structure:
128+
a. Random Number Generation: `r[name-of-the-distribution]`, e.g. `rnorm` for normal, `runif` for uniform.
129+
b. Density function: `d[name-of-the-distribution]`, e.g. `dnorm` for normal, `dunif` for uniform.
130+
c. Cumulative Distribution Function (CDF): `p[name-of-the-distribution]`, e.g. `pnorm` for normal, `punif` for uniform.
131+
d. Inverse (quantile) function: `q[name-of-the-distribution]`, e.g. `qnorm` for the normal, `qunif` for the uniform.
132+
133+
Here are some examples:
134+
135+
```{r random-numbers}
136+
# To ensure reproducibility
137+
set.seed(1231)
138+
139+
# 100,000 Unif(0,1) numbers
140+
x <- runif(1e5)
141+
hist(x)
142+
143+
# 100,000 N(0,1) numbers
144+
x <- rnorm(1e5)
145+
hist(x)
146+
147+
# 100,000 N(10,25) numbers
148+
x <- rnorm(1e5, mean = 10, sd = 5)
149+
hist(x)
150+
151+
# 100,000 Poisson(5) numbers
152+
x <- rpois(1e5, lambda = 5)
153+
hist(x)
154+
155+
# 100,000 rexp(5) numbers
156+
x <- rexp(1e5, 5)
157+
hist(x)
158+
```
159+
160+
More distributions available at `??Distributions`.
161+
162+
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/).
163+
164+
165+

0 commit comments

Comments
 (0)