forked from Robinlovelace/Creating-maps-in-R
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPractical-intro-to-R.Rmd
217 lines (150 loc) · 4.4 KB
/
Practical-intro-to-R.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
---
title: "A practical introduction to R"
author: "Robin Lovelace"
date: "March 27, 2015"
output: ioslides_presentation
---
```{r, include=FALSE}
pkgs <- c("png", "grid", "shiny", "knitr")
lapply(pkgs, library, character.only = T)
# opts_knit$set(root.dir = "~/repos/Creating-maps-in-R/")
```
# Installing and using the software
## Installing and using R and RStudio
- Install the latest version of R: http://cran.rstudio.com/
- Install the latest version of RStudio for your system:
http://www.rstudio.com/products/rstudio/download/
- Get acquainted to RStudio and the panels - play around!
http://dss.princeton.edu/training/RStudio101.pdf
```{r, echo=FALSE}
grid.raster(readPNG("figure/rstudio-proj.png"))
```
## Productivity with RStudio
Key shortcuts in RStudio:
```{r, echo=FALSE}
shortcuts <- data.frame(Command = c(
"Alt + Shift + K",
"Ctrl + Enter",
"Ctrl + R",
"Tab"),
Action = c("Show shortcuts",
"Run current line of code",
"Run all lines of code in the script",
"Autocomplete*"))
kable(shortcuts)
```
## Code used to generate that table
```{r, eval=FALSE}
shortcuts <- data.frame(Command = c(
"Alt + Shift + K",
"Ctrl + Enter",
"Ctrl + R",
"Tab"),
Action = c("Show shortcuts",
"Run current line of code",
"Run all lines of code in the script",
"Autocomplete*"))
kable(shortcuts)
```
## R packages
There are 7,000+ 'add-on' packages to 'supercharge' R.
Easiest way to install them, from RStudio:
```
Tools -> Install Packages
```
or using keyboard shortcuts:
```
Alt + T ... then k
```
## Install packages for this tutorial
Can be installed and loaded in 6 lines of code:
```{r, eval=FALSE}
pkgs <- c("devtools", "shiny", "rgdal", "rgeos", "ggmap", "raster")
install.packages(pkgs) # install the official packages!
library(devtools) # enables installation of leaflet
gh_pkgs <- c("rstudio/leaflet", "robinlovelace/stplanr")
install_github(gh_pkgs) # install packages on github
lapply(c(pkgs, "leaflet", "stplanr"), library, character.only = T)
```
```{r, include=FALSE}
pkgs <- c("devtools", "shiny", "rgdal", "rgeos", "ggmap", "raster")
library(devtools) # enables installation of leaflet
lapply(c(pkgs, "leaflet", "stplanr"), library, character.only = T)
```
# Create and plot data
## Basic data types
Anything that exists in R is an object. Let's create some with the `<-` symbol (`=` does the same job, before you ask!)
```{r}
vector_logical <- c(TRUE, TRUE, FALSE)
vector_character <- c("yes", "yes", "Hello!")
vector_numeric <- c(1, 3, 9.9)
class(vector_logical) # what are the other object classes?
```
Use the "Environment tab" (top right in RStudio) to see these
## Automating things
To ask R what objects it has, we can use `ls()`.
(Anything that happens is a function)
```{r}
ls()
```
Now we can automate the question: what class?
```{r}
obs <- ls()[grep("ve", ls())]
sapply(X = mget(obs), FUN = class)
```
## Getting help in R
To find out what just happened, we can use R's internal help
The most commonly used help functions are:
```{r}
help(apply) # get help on apply
?apply
?sapply
??apply
```
The `*apply` family of functions are R's internal `for` loops. What about `get()`
```{r}
?mget
```
## The data frame
The fundamental data object in R.
Create them with `data.frame()`
```{r}
data.frame(vector_logical, vector_character, n = vector_numeric)
```
Oops - we forgot to assign that. Tap `UP` or `Ctl-UP` in the console, then enter:
```{r}
df <- data.frame(vector_logical, vector_character, n = vector_numeric)
```
## Plotting data in R
`plot()` is polymorphic. Try `plot(df)` and `?plot`:
```{r, echo=FALSE}
plot(df) # note: now we can do things with the df
?plot # get help on plotting
```
## Subsetting data in R
The `[]` brackets, appending the object name, subset data.
A comma separates each dimension; nothing means everything:
```{r}
df[1,] # all of the the 1st line of df
```
In a 2d dataset, the following selects the 3rd line in the 3rd column:
```{r}
df[3,3]
```
## Making objects spatial
There are 4 fundamental `Spatial*` data types in R, enabled by
the foundational spatial package `sp`:
```{r, eval=FALSE}
SpatialPixels()
SpatialPoints()
SpatialLines()
SpatialPolygons()
```
These are `S4` R objects, with strictly defined `slots`.
All such `Spatial*` objects have `proj4string` and `bbox` slots.
Each can be extended as a `Spatial*DataFrame` to include data.
```{r}
lnd <- shapefile("data/london_sport.shp")
class(lnd)
leaflet() %>%
```