-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path20170930_RForbeginners.Rpres
467 lines (378 loc) · 11.4 KB
/
20170930_RForbeginners.Rpres
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
R-Ladies Istanbul: R For Beginners
========================================================
author: Hazel Kavili
date: 2017-09-30
width: 1400
height: 1280
font-family: 'Helvetica'
```{r, eval = TRUE, echo=FALSE, results=FALSE}
library(tidyverse)
```
R and R Studio
========================================================
Let's meet with the most used IDE (integrated desktop environment) for R
- Scripts, Console,
- Code and workflow are more reproducible if we can document everything that we do.
- Environment, History, Connections
- Files, Plots, Packages, Help, Viewer
- The viewer window will helpy ou to see Plots, Shiny applications, blog pages of you did!
Looking for Help!
========================================================
- Ask Google
- Search in [Stackoverflow](https://stackoverflow.com/questions/tagged/r)
- [An introduction to R](http://cran.us.r-project.org/doc/manuals/R-intro.pdf)
- [R for Data Science](http://r4ds.had.co.nz) book from Hadley Wickham & Garrett Grolemund
- [Try R](http://tryr.codeschool.com)
- R mailing list: first, learn how to ask questions!
- [R Tutorials](http://www.tutorialspoint.com/r/)
- Get help from R:
```{r, eval = FALSE}
help.start()
help(mean)
?mean
example(mean)
```
Getting Started - I
========================================================
### **Motor Trend Car Road Tests - simple dataset**
```{r}
head(mtcars)
```
```{r}
tail(mtcars)
```
Getting Started - II
========================================================
### **Teaching base or tidyverse way**
Base R codes:
```{r, eval = FALSE}
mtcars$transmission <-
ifelse(mtcars$am == 0, "automatic", "manual")
```
dplyr codes:
```{r, eval = FALSE}
mtcars <- mtcars %>%
mutate(transmission = case_when(am == 0 ~ "automatic", am == 1 ~ "manual"))
```
Getting Started - III
========================================================
### **Teaching base or tidyverse way**
#### **Visualisation**
Base R codes:
```{r, eval = FALSE}
mtcars$trans_color <-
ifelse(mtcars$transmission == "automatic", "green", "blue")
pdf("plots/scatter_base.pdf", width = 5, height = 3)
plot(mtcars$mpg ~ mtcars$disp, col = mtcars$trans_color)
legend("topright",
legend = c("automatic", "manual"),
pch = 1, col = c("green", "blue"))
dev.off()
```
ggplot codes:
```{r, eval = FALSE}
p1 <- ggplot(mtcars, aes(x = disp, y = mpg, color = transmission)) +
geom_point()
ggsave("plots/scatter_tidy.pdf", p1, width = 5, height = 3)
```
Getting Started - IV
========================================================
**R commands:**
- are case sensitive
- can be seperated either by a semi-colon(;), or by a newline
- #comment
**Objects:**
- varibables, arrays of numbers, character strings, functions
Getting Started - V
========================================================
**Assignment** and **Basic Operators**
- use **<-** for assigments
- +,-, *, /, ^, %%
**Logical Operators**
- <,>, <=, >=, ==, !=, !x, x & y, x | y
**Others**
- sum, sqrt, min, max, mean, var, sd, abs, summary
Most Frequently Used Objects
========================================================
- Vectors
- List
- Matrices
- Arrays
- Factors
- Data Frames
Vectors
========================================================
- use **c()** for concatenate more than one element.
- in programming vectors are variable sized sequence of values (not necessarily numbers).
```{r}
books <- c("history", "sci-fi", "fantasy")
print(books)
print(class(books))
```
```{r}
ages <- c(12,13,14,15,9,8)
print(ages)
print(class(ages))
```
Examples
========================================================
```{r}
#this is R-Ladies Istanbul
X <- 10
x <- 5
print(paste("X is", X))
print(paste("x is", x))
cat("X and x are equal? = ", X == x)
```
```{r}
myNumbers <- c(1:10) # c is short for concatenate
rep(myNumbers, times = 3)
twice <- rep(myNumbers, each = 2)
print(twice)
```
Examples - II
========================================================
```{r}
y <- c(1,2,3,10,15,20)
z <- c(y,4,5,6,y)
print(y)
```
- Vector Arithmetic
```{r}
5/y
5*y
y^2
sqrt(y)
```
Examples - III
========================================================
```{r}
seq(from = -10, to = 10, by = 1)
seq(from = 1, length = 25, by = 2 )
```
Simple Manipulations
========================================================
```{r}
weights <- c(55, 60, 45, 70, 56, 73, 59, 82)
sum(weights)
mean(weights)
sd(weights)
var(weights)
length(weights)
```
Lists
========================================================
- Lists can contain many different types of elements inside.
```{r}
myList <- list(c(1,2,3), 15, "hello")
print(myList)
```
- Select an element from lists
```{r}
myList[1]
myList[[1]][2]
myList[2]
```
Matrices
========================================================
- A matrix is **two-dimensional** recteangular data set.
```{r}
myMatrix <- matrix(c(1,2,3,4,5,6), nrow = 2, ncol = 3, byrow = TRUE)
print(myMatrix)
```
- Select an element from matrices
```{r}
myMatrix[1,2]
myMatrix[2,]
myMatrix[,1]
dim(myMatrix)
```
Matrices - II
========================================================
```{r}
myMatrix2 <- matrix(c(-6:-1), nrow = 2, ncol = 3, byrow = TRUE)
myMatrix + myMatrix2
```
```{r}
myMatrix %*% c(1,2,3)
diag(myMatrix2) #diagonal
t(myMatrix2) #transpose of matrix
```
Arrays
========================================================
- Arrays can be of any number of dimensions
```{r}
myArray <- array(c('uno','dos', 'tres'), dim = c(3,3,3))
print(myArray)
```
Factors
========================================================
- They are categorical variables.
- You can create using a vector. Factors stores the vector along with distinct values of the elements in the vector as labesl.
- The labels are always character irrespective of wheter it is numeric or character or Boolean etc. input vector. They are useful in statistical modelling.
```{r}
myVector <- c('blue', 'red', 'violet', 'red', 'red', 'blue')
print(myVector)
myVectorFactor <- factor(myVector)
print(myVectorFactor)
print(nlevels(myVectorFactor))
```
Tidy Data Concept and Data Frames
========================================================
**Tidy Data**
- Each variable forms a column
- Each observation forms a row
- Each type of observational unit forms a table
```{r}
mySurvey <- data.frame(
name = c("Harry", "Ron", "Hermione", "Draco", "Cedric"),
gender = c("Male", "Male", "Female", "Male", "Male"),
age = c(11, 11, 11, 11, 12),
bloodStatus = c("Half-blood", "Pure-blood","Muggle-born", "Pure-blood", NA),
house = c("Gryffindor", "Gryffindor", "Gryffindor", "Slytherin", NA)
)
print(mySurvey)
```
Indexing, Selecting and Modifying
========================================================
```{r}
is.na(mySurvey)
mySurvey[5,4] <- "Pure-blood"
str(mySurvey)
levels(mySurvey$house) <- c("Gryffindor", "Slytherin", "Hufflepuff")
mySurvey[5,5] <- "Hufflepuff"
print(mySurvey)
```
Indexing, Selecting and Modifying - II
========================================================
```{r}
mySurvey$name
head(mySurvey)
tail(mySurvey)
```
- look for:
```{r, eval = FALSE}
x %in% y, !(x %in% y), !x, !is.na()
```
R Packages
========================================================
- R has a lot of packages for you to make some work easily done!
- **CRAN** is the name of the R package repository but you can find and download many R packages on Github.
- These packages are about: statistics, modelling, visualisation, manipulating, documentation, making websites/applications etc.
Tidyverse
========================================================
- dplyr, broom, tidyr, lubridate
- ggplot2
- purrr, magrittr, forecats, tibble
- readxl
Installation
========================================================
```{r, eval = FALSE}
install.packages('tidyverse')
library(tidyverse)
```
Datasets
========================================================
- R has many example datasets and you can look at the list of datasets from [here](https://stat.ethz.ch/R-manual/R-devel/library/datasets/html/00Index.html) to make practice.
- Today we will use **20170930dataset** and learn how to load a data set on working directory and manipulate it with **dplyr** functions.
- To read a dataset, you can use these functions, according to your file type: *read.csv*, *read.table*, *read.xls*, *read.xlsx* etc.
- You need **path** of the file: where you store your file. For example *(/Users/hazelkavili/Desktop/R-LadiesIstanbul/20170930dataset.csv)*
```{r}
myDataset <- read.csv(file = "~/Desktop/R-LadiesIstanbul/20170930dataset.csv", sep = ",", header = TRUE)
print(myDataset)
```
Structure of Dataset
========================================================
```{r}
str(myDataset)
summary(myDataset)
```
Magical Words from dplyr!
========================================================
- **Variables(columns)**
- select
- mutate
- **Observations (rows)**
- filter
- arrange
- **Groups**
- summarise
*look for Hadley's book for more magical words*
%>% (Pipe) Operator
========================================================
- Basically tells R to take the value of that which is to the left and pass it to the right as an argument.
- cmd + shft + m
- kntr + shft + m
```{r}
library(dplyr)
myDataset %>% filter(MarketValue > 5) %>% summarise(Average = mean(Assets))
```
Select
========================================================
- Choosing is not losing!
```{r, eval = FALSE, warning = FALSE}
select(dataframe, var1, var2, ...)
select(dataframe, 1:4, -2)
```
```{r}
smallSet <- myDataset %>% select(Company, MarketValue)
print(smallSet)
```
- There are also helper functions: **starts_with**, **end_with**, **contains**
Mutate
========================================================
- Deals with info in your data which is not display
```{r, eval = FALSE, warning = FALSE}
mutate(dataframe, newVariable = var1 + var2)
mutate(dataframe, x = a + b, y = x + c)
```
```{r}
mutateSet <- myDataset %>% mutate(TotalMoney = Assets + Profits)
head(mutateSet)
```
Filter
========================================================
- Filter out rows, specific type of observation
```{r, eval = FALSE, warning = FALSE}
filter(dataframe, logicaltest)
```
```{r}
filteredSet <- myDataset %>% filter(Assets < 10 & Profits > 1)
print(filteredSet)
```
Arrange
========================================================
- Helps order observation (default ascending)
```{r, eval = FALSE, warning = FALSE}
arrange(dataframe, var1)
arrange(dataframe, var1, desc(var2))
```
```{r}
arrangedSet <- myDataset %>% select(GlobalRank, Company, MarketValue) %>% arrange(desc(MarketValue))
print(arrangedSet)
```
Summarise
========================================================
- Helps order observation (default ascending)
```{r, eval = FALSE, warning = FALSE}
summarise(dataframe, newVar = expression,. . .)
summarise(dataframe, sum = sum(A), avg = mean(B)..)
```
```{r}
summarisedSet <- myDataset %>% filter(grepl('Bank', Company) | grepl('bank', Company)) %>%
summarise(averageAssets = mean(Assets), averageSales = mean(Sales))
print(summarisedSet)
```
*look for grepl*
References
========================================================
- Mine Cetinkaya-Rundell's [Teaching Data Science to New Users](https://github.com/mine-cetinkaya-rundel/2017-07-05-teach-ds-to-new-user) presentation
- Ismail Sezen's [github](https://isezen.github.io/r-presentation/intro.html)
- [data.world](data.world) for many datasets
- Google :)
- [Data Carpentary](http://www.datacarpentry.org/)
========================================================
# <center> **Any questions?** </center>
#### - hazel@rladies.org & istanbul@rladies.org
#### - twitter.com/RLadiesIstanbul
#### - meetup.com/rladies-istanbul