-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathIntro_R.Rmd
667 lines (409 loc) · 13.2 KB
/
Intro_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
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
---
title: "R intro"
author: "Murray Cadzow"
date: "3 November 2015"
output: html_document
---
# Lesson 1
Covered in this lesson:
- what is R/RStudio
- getting help
- basic operations
- variables
- reading and writing data
- R scripts
## What is R/RStudio?
"R is a language and environment for statistical computing and graphics"
Commands are entered and run in the R commandline
RStudio is an interface for R
## Getting help
On the prompt you can type _??command_ or _help(command)_ and the documentation for the command will appear
You can also refer to the R manual https://cran.r-project.org/doc/manuals/r-release/R-intro.html
Or www.google.com with "R command" or "R thing I want to do"
## Basic operations
### Working directory
In R everything you do is in reference to what is know as your 'working directory'
This is the folder on your computer that R will read and write data to by default
You can find your current working directory with the command
```{r, eval=FALSE}
getwd()
```
likewise you can change your working directory with
```{r, eval=FALSE}
setwd(dir = "path/to/directory")
```
or in RStudio in the Session menu -> Set Working Driectory
### Operators
The basic arithmetic operatiors in R are:
- addition +
- subtraction -
- multiplication *
- division /
- parentheses ( )
- remainder division %%
- exponentiation ^
- assignment <-
Order of operations follows BEDMAS, with **assignment being evaluated last**
In your R prompt try the following:
```{r, eval = FALSE, echo=TRUE}
2 + 5
8 - 1
3 * 4
6 / 2
1 * ( 3 - 1 )
7 %% 6
2 ^ 3
a <- 5
```
## Variables
Variables are used in R to store things, these can be numbers, letters or even what other variables contain
It is good practice to name your variable so that it describes what it contains
Variables can be called anything you like so long as you follow these rules:
- must start with a letter
- cannot contain symbols other than underscore or period ( _ or .)
- numbers (except at start) and upper or lower case letters are allowed
- cannot be a reserved word
Examples of variable names
mydata
Mydata
myData
my_data
In the last example you used the assignment operator ( <- ) to store the value 5 in the variable called 'a'
To view what a variable contains you just enter the variable name into the R prompt
```{r, eval=TRUE, echo = FALSE}
a <- 5
```
```{r, eval=TRUE, echo = TRUE}
a
```
Variables can be overwritten by assigning something different to them
Variables only contain the last item assigned
```{r, eval = TRUE, echo = TRUE}
weight <- 64
height <- 168
weight
height
height <- height / 100
bmi <- weight / height ^ 2
```
What do weight, height, and bmi now contain?
```{r}
weight
height
bmi
```
## Reading and Writing data
### Reading
Just as we can store single values into variables, we can store entire datasets in a variable
Generally the data we want to read in will be in either tab or comma separated format
We have to specify the filename, if the file has a header at and the field seperating character ( \\t = tab )
If you don't assign the output from read.delim to a variable it will just ouput the entire file into your prompt
```{r, eval=FALSE, echo = TRUE}
# for tab separated data
mydata <- read.delim(file = "dummy_data.txt", header= TRUE, sep = "\t", stringsAsFactors = FALSE)
# for comma separated data
mydata <- read.delim(file = "dummy_data.csv", header = TRUE, sep = ",", stringsAsFactors = FALSE)
```
### Writing
Writing files is very similar with a few minor differences
```{r, eval = FALSE, echo = TRUE}
write.table(data = mydata, file = "mydata_out.txt", sep = "\t", col.names = TRUE, row.names= FALSE, quote = FALSE )
```
# Lesson 2
This lesson is on storing and accessing data in R
it will cover:
- viewing data
- data types
- data structures
- naming/indices
- gotchas
## Viewing data
First we need some data to look at
```{r, eval = TRUE, echo = TRUE}
mydata <- read.delim(file = "dummy_data.txt", header = TRUE, stringsAsFactors = FALSE, sep ="\t")
```
Lets look at the dimensions of this dataset
```{r, eval=FALSE, echo=TRUE}
dim(mydata)
```
This tells us how many rows and columns we are dealing with
We can also find out what data structure our data has been put into
```{r, eval = FALSE, echo = TRUE}
class(mydata)
```
We can see that our data has been put into a ```r class(mydata)``` structure. More on this later
To view the first entries of our data we use the _head()_ function
```{r, eval = FALSE, echo = TRUE}
head(mydata)
```
Similarily, we can view the last entries of our data we use the _tail()_ function
```{r, eval = FALSE, echo = TRUE}
tail(mydata)
```
In **RStudio** we can use the _View()_ function to open our data in a familiar spreadsheet view
```{r, eval=FALSE, echo = TRUE}
View(mydata)
```
## Data Types
In R there are different data types automatically assigned depending on your data
the main data types are:
- numeric
- integer
- logical
- character
- factor
- coercion
- missing values
### Numeric/Integer
Numeric and integer data types are exactly what they sound like - numbers
e.g. 1, 3.6 100, 5e7, 2e-2
The integer data type has to be specifically created
You can test if you have a numeric data type by using either the _class()_ or _is.numeric()_ functions
### Logical
The logical data type consists of the values **TRUE** and **FALSE**, and is often created as the result of the comparison operators (>,<,>=,<=,!,!=, ==, |, &)
### Character
A character or string of characters in R needs to be quoted, this is so R can distinguish between variable names and character data
Characters are any number,letter, or symbol on the keyboard including the white space (tab "\\t", space " ", and newline "\\n")
Placing a number in quotes forces it to be a character data type
eg "a", "a string of characters", "%", "2"
You can test if you have a character data type by using either the _class()_ or _is.character()_ functions
### Factors
Factors are a special data type in R and are used for groups with a set of levels
Levels can be either numeric or characters but being a factor data type, the possible __levels__/groups are stored too
In the _read.delim()_ function earlier we specified stringsAsFactors = FALSE, this is to override the default settings where all strings are read in as the factor data type
To demonstrate what a factor looks like we'll change a character data type into a factor with the _factor()_ function
```{r}
factor( c("control", "control", "case", "control", "case"))
factor( c(1,1,5,3,3,3,2))
```
To find the levels for a factor data type use the _levels()_ function
### Coercion
You can coerce (convert) data types by using the as.\<datatype\>() function
A common conversion is numeric or factor -> character
Some data can't be coerced because it doesn't make sense
e.g.
```{r, echo = TRUE, eval = TRUE}
# numeric to character
a <- 4
class(a)
a
a <- as.character(a)
class(a)
a
# doesn't make sense
word <- "letters"
class(word)
word
word <- as.numeric(word)
class(word)
word
```
### Missing values
Missing values in R are assigned a special datatype called **NA**
NA acts as a place holder but often causes errors in functions when not removed
R provides a few useful functions for dealing with NA
```{r, echo = TRUE, eval = TRUE}
missing <- NA
missing
is.na(missing)
mydata <- c(1, 2, NA, 3, 4)
# example error for missing data
sum(data = mydata)
```
You can see that if you have an **NA** in your data, you will get back **NA** as a result
We can either remove the NA prior to using it, or functions often have the option to temporarily ignore NAs using na.rm = TRUE
```{r, echo = TRUE, eval = TRUE}
mydata
sum( data = mydata, na.rm = TRUE )
mydata <- na.omit(mydata)
mydata
sum(data = mydata)
```
## Data Structures
In R we arrange multiple data into structures based on the content
These structures include:
- vectors
- matrices
- lists
- dataframes
The most common of these are vectors and dataframes
### Vectors
vectors are the most basic data structure in R and consist of 0 (empty vector) or more values of any data type
Vectors are created using the function _c()_ - short for concatenate
```{r, echo = TRUE, eval = TRUE}
# an empty vector
c()
# numeric vector
c(1,2,3,4,5)
# logical vector
c(TRUE, FALSE, TRUE)
# character vector
c("a", "b", "c")
# a multitype vector
c(1, "a", FALSE)
```
We can also find out how many items are in a vector with the _length()_ function
```{r, echo = TRUE, eval = TRUE}
mydata <- c(1,5,3,4,2)
length(mydata)
```
### Matrices
A matrix is a 2 dimensional rectangular layout of data elements of a single data type
```{r, echo = TRUE, eval = TRUE}
mat <- matrix( data = c(1,2,3,4,5,6), nrow = 2, ncol = 3, byrow = TRUE)
mat
```
### Lists
A list consists of 0 or more elements, each element can be of same or differing lengths as well as differing data type and/or structure
```{r, eval = TRUE, echo = TRUE}
# empty list
list()
# 2 vectors
list1 <- list( c(1,2,3), c('a', 'b'))
list1
# vector and matrix
list2 <- list(mydata, mat)
list2
```
### Dataframes
Dataframes can be thought of as similar to a spreadsheet, made up of vectors forming columns of the same length. The number length of the vectors is the number of rows
```{r, eval = TRUE, echo = TRUE}
height <- c(1.5, 1.6, 1.55)
weight <- c(60,63,62)
df <- data.frame(height, weight )
df
# number rows
nrow(df)
# number of columns
ncol(df)
# dimensions (rows, cols)
dim(df)
```
## Naming/Indices
### Naming
We can find out different attributes from our dataframe such as dimensions
```{r, eval = TRUE, echo = TRUE}
# names of columns
names(df)
# column names
colnames(df)
# row names
rownames(df)
```
You can also change the names, row names, column names
The replacement vector has to match the length of the current names vector
```{r, eval = TRUE, echo = TRUE}
names(df) <- c("height_m", "weight_kg")
names(df)
rownames(df) <- c("row1", "row2", "row3")
rownames(df)
```
You can also use these column names to access only that column
To do this we use the variable name followed by the "$" symbol followed by the column name
```{r, echo = TRUE, eval = TRUE}
df$height_m
```
### Indices
In R, each item can also be refered to by its position (i) using the square brackets [i]
Vectors only have a single index, whereas matrices and dataframes have 2 in the form [row, column]
Lists are special in that [[i]] is used to access the list element
```{r, echo = TRUE, eval = TRUE}
dim(df)
df
# first row, first column
df[1,1]
# third row, first column
df[3,1]
# first and second rows, second column
df[c(1,2), 2]
# example error: second row, third column
df[2,3]
```
If you leave a index blank then R gives you everything
```{r, eval=TRUE, echo=TRUE}
# all of row 1
df[1, ]
# all of column 1
df[, 1]
# everything
df[ , ]
```
R also has a special feature for generating a vector of sequences of numbers with a:b
We can use this feature to select multiple sequencial items
```{r, eval = TRUE, echo = TRUE}
1:3
5:9
# select rows 1 to 3, column 2
df[1:3, 2]
```
## Gotchas
- blanks not becoming NAs
- auto factorisation when reading data in
- NAs being used by a function
- data not being the expected type by function
- index doesn't exist
# Lesson 3
In this lesson we will cover subsetting your data using:
- logical operations
- subset()
- square brackets
## Logical operations
Before we can subset we need to know how to create the conditions for R to know what to select
We do this through logical comparisions
The logical operators as mentioned before are:
- == ( equality )
- != ( not equal)
- \> (greater than)
- \>= (greater than or equal to)
- < (less than)
- <= (less than or equal to)
- ! (not)
- \| (or)
- & (and)
The result of using these logical comparisons is a vector of **TRUE**s and **FALSE**s
Here are some examples:
```{r, eval = TRUE, echo = TRUE}
df$weight
df$weight_kg < 62
# using not
!( df$weight_kg < 62)
# equality
df$weight_kg == 62
# not equality
df$weight_kg != 62
# combine conditions
df$weight > 62 | df$weight < 62
# different types
3 < 'a'
```
**NOTE** comparisons will be performed on the "lowest common data type" which is usually _character_ when different types are involved
## _Subset()_
The subset function provides a easy way to subset your data into groups based on your logical conditions
We'll use the dummy data for these examples
```{r, echo = TRUE, eval = TRUE}
# read data in
mydata <- read.delim(file = "dummy_data.txt", header = TRUE, stringsAsFactors = FALSE, sep ="\t")
# what does it look like?
dim(mydata)
head(mydata)
# select patients under 40
under40 <- subset(x = mydata, mydata$AGECOL < 40)
# how many people do we now have?
dim(under40)
# from our original data how many people are male (SEX == 1) and have gout (GOUTAFFSTAT == 2)
male_gout <- subset(x = mydata, mydata$SEX == 1 & mydata$GOUTAFFSTAT == 2)
dim(male_gout)
```
### Subset with square brackets
Just as we can use square brackets and indices to select items we want, instead of indices we can provide a logical condition
```{r, echo = TRUE, eval = TRUE}
# repeat under 40 selection
# here we're saying we want all the columns but select the rows where the condition is met
under40_repeat <- mydata[mydata$AGECOL < 40 , ]
dim(under40_repeat)
# who is missing age?
missing_age <- mydata[is.na(mydata$AGECOL),"PATIENT"]
missing_age
```
# Lesson 4
Data exploration