-
Notifications
You must be signed in to change notification settings - Fork 12
/
25-starting-with-data.Rmd
990 lines (728 loc) · 32.8 KB
/
25-starting-with-data.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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
# Starting with data {#sec-startdata}
**Learning Objectives**
- Describe what a data frame is.
- Load external data from a .csv file into a data frame.
- Summarize the contents of a data frame.
- Describe what a factor is.
- Convert between strings and factors.
- Reorder and rename factors.
- Format dates.
- Other R objects: matrices and lists
- Export tabular data.
## Presentation of the gene expression data
We are going to use part of the data published by [Blackmore *et al.*
(2017)](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5544260/), *The
effect of upper-respiratory infection on transcriptomic changes in the
CNS*. The goal of the study was to determine the effect of an
upper-respiratory infection on changes in RNA transcription occuring
in the cerebellum and spinal cord post infection. Gender matched eight
week old C57BL/6 mice were inoculated saline or with Influenza A by
intranasal route and transcriptomic changes in the cerebellum and
spinal cord tissues were evaluated by RNA-seq at days 0
(non-infected), 4 and 8.
The dataset is stored as a comma separated value (CSV) file. Each row
holds information for a single RNA expression measurement, and the
columns represent:
| Column | Description |
|------------|----------------------------------------------------------------------------------------------|
| gene | The name of the gene that was measured |
| sample | The name of the sample the gene expression was measured in |
| expression | The value of the gene expression |
| organism | The organism/species - here all data stem from mice |
| age | The age of the mouse (all mice were 8 weeks here) |
| sex | The sex of the mouse |
| infection | The infection state of the mouse, i.e. infected with Influenza A or not infected. |
| strain | The Influenza A strain; C57BL/6 in all cases. |
| time | The duration of the infection (in days). |
| tissue | The tissue that was used for the gene expression experiment, i.e. cerebellum or spinal cord. |
| mouse | The mouse unique identifier. |
| ENTREZID | The gene ID for the ENTREZ database |
| product | The gene product |
| ensembl_gene_id | The ID of the gene from the ENSEMBL database |
| external_synonym | A name synonym for the gene |
| chromosome_name | The chromosome name of the gene |
| gene_biotype | The gene biotype |
| phenotype_description | The phenotype description of the gene |
| hsapiens_homolog_associated_gene_name| The human homologous gene |
## Reading tabular data
We are going to use the R function `download.file()` to download the
CSV file that contains the gene expression data, and we will use
`read.csv()` to load into memory the content of the CSV file as an
object of class `data.frame`. Inside the `download.file()` function,
the first entry is a character string with the source URL. This source
URL downloads a CSV file from a GitHub repository. The text after the
comma (`"data/rnaseq.csv"`) is the destination of the file on your
local machine. You'll need to have a folder on your machine called
`"data"` where you'll download the file. So this command downloads the
remote file, names it `"rnaseq.csv"` and adds it to a preexisting
folder named `"data"`.
```{r, eval = FALSE}
download.file(url = "https://raw.githubusercontent.com/UCLouvain-CBIO/WSBIM1207/master/data/rnaseq.csv",
destfile = "data/rnaseq.csv")
```
Alternatively, you can download the
[file](https://raw.githubusercontent.com/UCLouvain-CBIO/WSBIM1207/master/data/rnaseq.csv)
manually and move it in the data directory in your RStudio
project. This approach however has the drawback of loosing the
provenance of the data.
You are now ready to load the data:
```{r, eval = TRUE}
rna <- read.csv("data/rnaseq.csv")
```
This statement doesn't produce any output because, as you might
recall, assignments don't display anything. If we want to check that
our data has been loaded, we can see the contents of the data frame by
typing its name:
```{r, eval = FALSE}
rna
```
Wow... that was a lot of output. At least it means the data loaded
properly. Let's check the top (the first 6 lines) of this data frame
using the function `head()`:
```{r}
head(rna)
## Try also
## View(rna)
```
**Note**
`read.csv()` assumes that fields are delineated by commas, however, in
several countries, the comma is used as a decimal separator and the
semicolon (;) is used as a field delineator. If you want to read in
this type of files in R, you can use the `read.csv2()` function. It
behaves exactly like `read.csv()` but uses different parameters for
the decimal and the field separators. If you are working with another
format, they can be both specified by the user. Check out the help for
`read.csv()` by typing `?read.csv` to learn more. There is also the
`read.delim()` for in tab separated data files. It is important to
note that all of these functions are actually wrapper functions for
the main `read.table()` function with different arguments. As such,
the data above could have also been loaded by using `read.table()`
with the separation argument as `,`. The code is as follows:
```{r, eval = TRUE}
rna <- read.table(file = "data/rnaseq.csv",
sep = ",", quote = "\"",
header = TRUE)
```
The header argument has to be set to `TRUE` to be able to read the
headers as by default `read.table()` has the header argument set to
FALSE. The quote argument has to be set to `"\""` to only allow " as a
quoting character (see how the `product` variable for gene `Rtca` in
`rnaseq.csv` is written).
## What are data frames?
Data frames are the _de facto_ data structure for most tabular data,
and what we use for statistics and plotting.
A data frame can be created by hand, but most commonly they are
generated by the functions `read.csv()` or `read.table()`; in other
words, when importing spreadsheets from your hard drive (or the web).
A data frame is the representation of data in the format of a table
where the columns are vectors that all have the same length. Because
columns are vectors, each column must contain a single type of data
(e.g., characters, integers, factors). For example, here is a figure
depicting a data frame comprising a numeric, a character, and a
logical vector.
![](./figs/data-frame.svg)
We can see this when inspecting the <b>str</b>ucture of a data frame
with the function `str()`:
```{r}
str(rna)
```
## Inspecting `data.frame` Objects
We already saw how the functions `head()` and `str()` can be useful to
check the content and the structure of a data frame. Here is a
non-exhaustive list of functions to get a sense of the
content/structure of the data. Let's try them out!
**Size**:
- `dim(rna)` - returns a vector with the number of rows in the first
element, and the number of columns as the second element (the
**dim**ensions of the object)
- `nrow(rna)` - returns the number of rows
- `ncol(rna)` - returns the number of columns
**Content**:
- `head(rna)` - shows the first 6 rows
- `tail(rna)` - shows the last 6 rows
**Names**:
- `names(rna)` - returns the column names (synonym of `colnames()` for
`data.frame` objects)
- `rownames(rna)` - returns the row names
**Summary**:
- `str(rna)` - structure of the object and information about the
class, length and content of each column
- `summary(rna)` - summary statistics for each column
Note: most of these functions are "generic", they can be used on other types of
objects besides `data.frame`.
`r msmbstyle::question_begin()`
Based on the output of `str(rna)`, can you answer the following
questions?
- What is the class of the object `rna`?
- How many rows and how many columns are in this object?
`r msmbstyle::question_end()`
`r msmbstyle::solution_begin()`
- class: data frame
- how many rows: `r nrow(rna)`
- how many columns: `r ncol(rna)`
`r msmbstyle::solution_end()`
## Indexing and subsetting data frames
Our `rna` data frame has rows and columns (it has 2 dimensions), if we
want to extract some specific data from it, we need to specify the
"coordinates" we want from it. Row numbers come first, followed by
column numbers. However, note that different ways of specifying these
coordinates lead to results with different classes.
```{r, eval = FALSE}
# first element in the first column of the data frame (as a vector)
rna[1, 1]
# first element in the 6th column (as a vector)
rna[1, 6]
# first column of the data frame (as a vector)
rna[, 1]
# first column of the data frame (as a data.frame)
rna[1]
# first three elements in the 7th column (as a vector)
rna[1:3, 7]
# the 3rd row of the data frame (as a data.frame)
rna[3, ]
# equivalent to head_rna <- head(rna)
head_rna <- rna[1:6, ]
head_rna
```
`:` is a special function that creates numeric vectors of integers in
increasing or decreasing order, test `1:10` and `10:1` for
instance. See section \@ref(sec-genvec) for details.
You can also exclude certain indices of a data frame using the "`-`" sign:
```{r, eval = FALSE}
rna[, -1] ## The whole data frame, except the first column
rna[-c(7:32428), ] ## Equivalent to head(rna)
```
Data frames can be subset by calling indices (as shown previously),
but also by calling their column names directly:
```{r, eval = FALSE}
rna["gene"] # Result is a data.frame
rna[, "gene"] # Result is a vector
rna[["gene"]] # Result is a vector
rna$gene # Result is a vector
```
In RStudio, you can use the autocompletion feature to get the full and
correct names of the columns.
When we inspect the elements of the column
`hsapiens_homolog_associated_gene_name` (for example with `View(rna)`),
we can see that some cells contain NA values. If we wanted to extract
only mouse genes of this table that have a human homologous,
we could combine `is.na()` and data frames subsetting:
```{r, eval = TRUE}
is_missing_hsapiens_homolog <- is.na(rna$hsapiens_homolog_associated_gene_name)
rna_hsapiens_homolog <- rna[!is_missing_hsapiens_homolog,]
```
```{r, eval = TRUE}
head(rna_hsapiens_homolog)
anyNA(rna_hsapiens_homolog$hsapiens_homolog_associated_gene_name)
```
`r msmbstyle::question_begin()`
How many mouse genes do not have a human homologous?
`r msmbstyle::question_end()`
`r msmbstyle::solution_begin()`
```{r}
sum(is_missing_hsapiens_homolog)
table(is_missing_hsapiens_homolog)
```
`r msmbstyle::solution_end()`
`r msmbstyle::question_begin()`
1. Create a `data.frame` (`rna_200`) containing only the data in
row 200 of the `rna` dataset.
2. Notice how `nrow()` gave you the number of rows in a `data.frame`?
- Use that number to pull out just that last row in the initial
`rna` data frame.
- Compare that with what you see as the last row using `tail()` to
make sure it's meeting expectations.
- Pull out that last row using `nrow()` instead of the row number.
- Create a new data frame (`rna_last`) from that last row.
3. Use `nrow()` to extract the row that is in the middle of the
`rna` dataframe. Store the content of this row in an object
named `rna_middle`.
4. Combine `nrow()` with the `-` notation above to reproduce the
behavior of `head(rna)`, keeping just the first through 6th
rows of the rna dataset.
`r msmbstyle::question_end()`
`r msmbstyle::solution_begin()`
```{r}
## 1.
rna_200 <- rna[200, ]
## 2.
## Saving `n_rows` to improve readability and reduce duplication
n_rows <- nrow(rna)
rna_last <- rna[n_rows, ]
## 3.
rna_middle <- rna[n_rows / 2, ]
## 4.
rna_head <- rna[-(7:n_rows), ]
```
`r msmbstyle::solution_end()`
## Exporting tabular data {#sec-export}
We have seen how to read a text-based spreadsheet into R using the
`read.table` family of functions. To export a `data.frame` to a
text-based spreadsheet, we can use the `write.table` set of functions
(`write.csv`, `write.delim`, ...). They all take the variable to be
exported and the file to be exported to. For example, to export the
`rna` data to the `my_rnaseq.csv` file in the `data_output`
directory, we would execute:
```{r, eval = FALSE}
write.csv(rna, file = "data_output/my_rnaseq.csv")
```
This new csv file can now be shared with other collaborators who
aren't familiar with R.
## Factors
Factors represent **categorical data**. They are stored as integers
associated with labels and they can be ordered or unordered. While
factors look (and often behave) like character vectors, they are
actually treated as integer vectors by R. So you need to be very
careful when treating them as strings.
Once created, factors can only contain a pre-defined set of values,
known as *levels*. By default, R always sorts levels in alphabetical
order. For instance, if you have a factor with 2 levels:
```{r}
sex <- factor(c("male", "female", "female", "male", "female"))
```
R will assign `1` to the level `"female"` and `2` to the level
`"male"` (because `f` comes before `m`, even though the first element
in this vector is `"male"`). You can see this by using the function
`levels()` and you can find the number of levels using `nlevels()`:
```{r}
levels(sex)
nlevels(sex)
```
`r msmbstyle::question_begin()`
- How many genes have been measured in this experiment?
`r msmbstyle::question_end()`
`r msmbstyle::solution_begin()`
```{r}
nlevels(factor(rna$gene))
```
`r msmbstyle::solution_end()`
Sometimes, the order of the factors does not matter, other times you
might want to specify the order because it is meaningful (e.g., "low",
"medium", "high"), it improves your visualization, or it is required
by a particular type of analysis. Here, one way to reorder our levels
in the `sex` vector would be:
```{r}
sex ## current order
sex <- factor(sex, levels = c("male", "female"))
sex ## after re-ordering
```
In R's memory, these factors are represented by integers (1, 2, 3),
but are more informative than integers because factors are self
describing: `"female"`, `"male"` is more descriptive than `1`,
`2`. Which one is "male"? You wouldn't be able to tell just from the
integer data. Factors, on the other hand, have this information built
in. It is particularly helpful when there are many levels (like the
species names in our example dataset).
### Converting to characters {-}
If you need to convert a factor to a character vector, you use
`as.character(x)`.
```{r}
as.character(sex)
# try also: as.numeric(sex)
```
<!-- ### Numeric factors {-} -->
<!-- Converting factors where the levels appear as numbers (such as -->
<!-- concentration levels, or years) to a numeric vector is a little -->
<!-- trickier. The `as.numeric()` function returns the index values of the -->
<!-- factor, not its levels, so it will result in an entirely new (and -->
<!-- unwanted in this case) set of numbers. One method to avoid this is to -->
<!-- convert factors to characters, and then to numbers. Another method is -->
<!-- to use the `levels()` function. Compare: -->
<!-- ```{r} -->
<!-- year_fct <- factor(c(1990, 1983, 1977, 1998, 1990)) -->
<!-- as.numeric(year_fct) ## Wrong! And there is no warning... -->
<!-- as.numeric(as.character(year_fct)) ## Works... -->
<!-- as.numeric(levels(year_fct))[year_fct] ## The recommended way. -->
<!-- ```
<!-- Notice that in the `levels()` approach, three important steps occur: -->
<!-- * We obtain all the factor levels using `levels(year_fct)` -->
<!-- * We convert these levels to numeric values using `as.numeric(levels(year_fct))` -->
<!-- * We then access these numeric values using the underlying integers of the -->
<!-- vector `year_fct` inside the square brackets -->
### Renaming factors {-}
When your data is stored as a factor, you can use the `plot()`
function to get a quick glance at the number of observations
represented by each factor level. Let's look at the number of males
and females in our data.
```{r firstfactorplot, fig.cap = "Bar plot of the number of females and males."}
plot(sex)
```
If we want to rename these factor, it is sufficient to change its
levels:
```{r}
levels(sex)
levels(sex) <- c("M", "F")
sex
plot(sex)
```
`r msmbstyle::question_begin()`
- Rename "female" and "male" to "Female" and "Male" respectively.
`r msmbstyle::question_end()`
`r msmbstyle::question_begin()`
We have seen how data frames are created when using `read.csv()`, but
they can also be created by hand with the `data.frame()` function.
There are a few mistakes in this hand-crafted `data.frame`. Can you
spot and fix them? Don't hesitate to experiment!
```{r, eval=FALSE}
animal_data <- data.frame(
animal = c(dog, cat, sea cucumber, sea urchin),
feel = c("furry", "squishy", "spiny"),
weight = c(45, 8 1.1, 0.8))
```
`r msmbstyle::question_end()`
`r msmbstyle::solution_begin()`
- missing quotations around the names of the animals
- missing one entry in the "feel" column (probably for one of the furry animals)
- missing one comma in the weight column
`r msmbstyle::solution_end()`
`r msmbstyle::question_begin()`
Can you predict the class for each of the columns in the following
example?
Check your guesses using `str(country_climate)`:
- Are they what you expected? Why? Why not?
- Try again by adding `stringsAsFactors = TRUE` after the last
variable when creating the data frame? What is happening now?
`stringsAsFactors` can also be set when reading text-based
spreadsheets into R using `read.csv()`.
```{r, eval = FALSE}
country_climate <- data.frame(
country = c("Canada", "Panama", "South Africa", "Australia"),
climate = c("cold", "hot", "temperate", "hot/temperate"),
temperature = c(10, 30, 18, "15"),
northern_hemisphere = c(TRUE, TRUE, FALSE, "FALSE"),
has_kangaroo = c(FALSE, FALSE, FALSE, 1)
)
```
`r msmbstyle::question_end()`
The automatic conversion of data type is sometimes a blessing, sometimes an
annoyance. Be aware that it exists, learn the rules, and double check that data
you import in R are of the correct type within your data frame. If not, use it
to your advantage to detect mistakes that might have been introduced during data
entry (a letter in a column that should only contain numbers for instance).
Learn more in this [RStudio
tutorial](https://support.rstudio.com/hc/en-us/articles/218611977-Importing-Data-with-RStudio)
## Matrices
Before proceeding, now that we have learnt about dataframes, let's
recap package installation and learn about a new data type, namely the
`matrix`. Like a `data.frame`, a matrix has two dimensions, rows and
columns. But the major difference is that all cells in a `matrix` must
be of the same type: `numeric`, `character`, `logical`, ... In that
respect, matrices are closer to a `vector` than a `data.frame`.
The default constructor for a matrix is `matrix`. It takes a vector of
values to populate the matrix and the number of row and/or
columns[^ncol]. The values are sorted along the columns, as illustrated
below but you can also sort them along the row with the argument `byrow = TRUE`.
```{r mat1}
m <- matrix(1:9, ncol = 3, nrow = 3)
m
# try now with byrow = TRUE
```
[^ncol]: Either the number of rows or columns are enough, as the other
one can be deduced from the length of the values. Try out what happens
if the values and number of rows/columns don't add up.
```{r pkg_qst, echo = FALSE}
msmbstyle::question(text = "Using the function `installed.packages()`, create a `character` matrix containing the information about all packages currently installed on your computer. Explore it.")
```
`r msmbstyle::solution_begin()`
```{r pkg_sln, eval = FALSE}
## create the matrix
ip <- installed.packages()
head(ip)
## try also View(ip)
## number of package
nrow(ip)
## names of all installed packages
rownames(ip)
## type of information we have about each package
colnames(ip)
```
`r msmbstyle::solution_end()`
It is often useful to create large random data matrices as test
data. The exercise below asks you to create such a matrix with random
data drawn from a normal distribution of mean 0 and standard deviation
1, which can be done with the `rnorm()` function.
```{r rnormmat_qst, echo = FALSE}
msmbstyle::question(text = "Construct a matrix of dimension 1000 by 3 of normally distributed data (mean 0, standard deviation 1).")
```
`r msmbstyle::solution_begin()`
```{r rnormmat_sln}
set.seed(123)
m <- matrix(rnorm(3000), ncol = 3)
dim(m)
head(m)
```
`r msmbstyle::solution_end()`
## Formatting Dates
One of the most common issues that new (and experienced!) R users have
is converting date and time information into a variable that is
appropriate and usable during analyses.
### Note on dates in spreadsheet programs {-}
Dates in spreadsheets are generally stored in a single column. While
this seems the most natural way to record dates, it actually is not
best practice. A spreadsheet application will display the dates in a
seemingly correct way (to a human observer) but how it actually
handles and stores the dates may be problematic. It is often much
safer to store dates with YEAR, MONTH and DAY in separate columns or
as YEAR and DAY-OF-YEAR in separate columns.
Spreadsheet programs such as LibreOffice, Microsoft Excel, OpenOffice,
Gnumeric, ... have different (and often incompatible) ways of encoding
dates (even for the same program between versions and operating
systems). Additionally, Excel can [turn things that aren't dates into
dates](https://nsaunders.wordpress.com/2012/10/22/gene-name-errors-and-excel-lessons-not-learned/)
(@Zeeberg:2004), for example names or identifiers like MAR1, DEC1,
OCT4. So if you're avoiding the date format overall, it's easier to
identify these issues.
The [Dates as
data](https://datacarpentry.org/spreadsheet-ecology-lesson/03-dates-as-data/index.html)
section of the Data Carpentry lesson provides additional insights
about pitfalls of dates with spreadsheets.
We are going to use the `ymd()` function from the package
**`lubridate`** (that belongs to the **`tidyverse`**, which we'll
focus on in the next chapter). **`lubridate`** gets installed as part
as the **`tidyverse`** installation. Let's load it with
```{r loadlibridate, message=FALSE}
library("lubridate")
```
`ymd()` takes a vector representing year, month, and day, and converts
it to a `Date` vector. `Date` is a class of data recognized by R as
being a date and can be manipulated as such. The argument that the
function requires is flexible, but, as a best practice, is a character
vector formatted as "YYYY-MM-DD".
Let's create a date object and inspect its structure:
```{r}
my_date <- ymd("2015-01-01")
str(my_date)
```
Now let's paste the year, month, and day separately - we get the same result:
```{r}
# sep indicates the character to use to separate each component
my_date <- ymd(paste("2015", "1", "1", sep = "-"))
str(my_date)
```
Let's now familiarise ourselves with a typical date manipulation
pipeline. The small data below has stored dates in different `year`,
`month` and `day` columns.
```{r}
x <- data.frame(year = c(1996, 1992, 1987, 1986, 2000, 1990, 2002, 1994, 1997, 1985),
month = c(2, 3, 3, 10, 1, 8, 3, 4, 5, 5),
day = c(24, 8, 1, 5, 8, 17, 13, 10, 11, 24),
value = c(4, 5, 1, 9, 3, 8, 10, 2, 6, 7))
x
```
Now we apply this function to the `x` dataset. We first dreate a
character vector from the `year`, `month`, and `day` columns of `x`
using `paste()`:
```{r}
paste(x$year, x$month, x$day, sep = "-")
```
This character vector can be used as the argument for `ymd()`:
```{r}
ymd(paste(x$year, x$month, x$day, sep = "-"))
```
The resulting `Date` vector can be added to `x` as a new column called `date`:
```{r}
x$date <- ymd(paste(x$year, x$month, x$day, sep = "-"))
str(x) # notice the new column, with 'date' as the class
```
Let's make sure everything worked correctly. One way to inspect the
new column is to use `summary()`:
```{r}
summary(x$date)
```
Note that `ymd()` expects to have the year, month and day, in that
order. If you have for instance day, month and year, you would need
`dmy()`.
```{r}
dmy(paste(x$day, x$month, x$month, sep = "-"))
```
`lubdridate` has many functions to address all date variations.
## Lists
A data type that we haven't seen yet, but that is useful to know are
lists:
- **`list`**: one dimension, every item can be of a different data
type.
Below, let's create a list containing a vector of numbers, characters,
a matrix, a dataframe and another list:
```{r list0}
l <- list(1:10, ## numeric
letters, ## character
installed.packages(), ## a matrix
cars, ## a data.frame
list(1, 2, 3)) ## a list
length(l)
str(l)
```
List subsetting is done using `[]` to subset a new sub-list or `[[]]`
to extract a single element of that list (using indices or names, of
the list is named).
```{r}
l[[1]] ## first element
l[1:2] ## a list of length 2
l[1] ## a list of length 1
```
`r msmbstyle::question_begin()`
You can also attribute a name to each element of a list. Give the
following names (in that order) to the list elements with the function
`names()`: `"numbers"`, `"alphabet"`, `"installed_packages"`, `"cars"`
and `"random"`. Check if the names were correctly attributed.
`r msmbstyle::question_end()`
`r msmbstyle::solution_begin()`
```{r}
names(l) <- c("numbers", "alphabet", "installed_packages",
"cars", "random")
names(l)
```
`r msmbstyle::solution_end()`
`r msmbstyle::question_begin()`
Subset the previously defined list `l`, to keep:
- a list with all but the matrix of installed packages
- a list containing only the `cars` data.frame
- the `cars` data.frame
`r msmbstyle::question_end()`
`r msmbstyle::solution_begin()`
```{r}
## list with all but the matrix of installed packages
l[-3]
## a list containing only the `cars` data.frame
l["cars"]
# equivalent to
l[4]
## the `cars` data.frame
l[["cars"]]
# equivalent to
l[[4]]
```
`r msmbstyle::solution_end()`
## Summary of R objects
So far, we have seen several types of R object varying in the number
of dimensions and whether they could store a single or multiple data
types:
- **`vector`**: one dimension (they have a length), single type of data.
- **`matrix`**: two dimensions, single type of data.
- **`data.frame`**: two dimensions, one type per column.
- **`list`**: one dimension (length), any type per element.
## Additional exercises
`r msmbstyle::question_begin()`
You're doing an colony counting experiment, counting every day how
many molds you see in your cell cultures.
- Create a vector named `molds` containing the results of your counts:
1, 2, 5, 8 and 10. Create a vector `days` containing the week day,
from Monday to Friday. Use these two vector to create a `data.frame`
named `molds_study` containing two variables, `Day` and
`Molds_count`.
- Create a new `data.frame` that contains the observations where more
than 2 colonies were counted. How many observations are there? How
many counts are there in total for these observations.
You repeat the molds study experiment the following week and count the
following numbers of molds: 1, 6, 6, 5 and 4.
- Add these data as a third column to the `molds_study` `data.frame`
and rename the variables as `Day`, `Molds_1` and `Molds_2`.
- Calculate for each experiment the total number of molds
counted. Check if the first experiment counted more molds than the
second one.
- Save the `molds_study` variable in a file named `molds_study.rda`.
`r msmbstyle::question_end()`
<!-- `r msmbstyle::solution_begin()` -->
<!-- ```{r} -->
<!-- molds <- c(1, 2, 5, 8, 10) -->
<!-- days <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday") -->
<!-- molds_study <- data.frame("Day" = days, "Molds_count" = molds) -->
<!-- ``` -->
<!-- ```{r} -->
<!-- molds2 <- molds_study[molds_study$Molds_count > 2, ] -->
<!-- nrow(molds2) -->
<!-- sum(molds2$Molds_count) -->
<!-- ``` -->
<!-- ```{r} -->
<!-- molds_study$Molds_count2 <- c(1, 6, 6, 5, 4) -->
<!-- names(molds_study)[2:3] <- c("Molds_1", "Molds_2") -->
<!-- ``` -->
<!-- ```{r} -->
<!-- total1 <- sum(molds_study$Molds_1) -->
<!-- total2 <- sum(molds_study$Molds_2) -->
<!-- total1 > total2 -->
<!-- ``` -->
<!-- `r msmbstyle::solution_end()` -->
`r msmbstyle::question_begin()`
We are going to analyse beer consumption in 48 individuals. The data
are available in the `rWSBIM1207` package. The data illustrated the
fictive beer consumption in liters per year at different age according
to gender and employment.
- Load the `rWSBIM1207` package. If the package isn't installed of its
version is greater than 0.1.1, install it from the
`UCLouvain-CBIO/rWSBIM1207` GitHub repository using the
`BiocManager::install()` (or `remotes::install_github()`)
function. If you use a recent Renku enironment, the package is
already available.
- Using the `beers.csv()` function from `rWSBIM1207`, find the path
the `beers.csv` file and read it to produce a `data.frame` named
`beers`. The spreadsheet uses semi-colons `;` to separate cells. Use
`read.csv2()` and `read.delim()` and set the separator
appropriately, and verify that the two variables are identical.
- Check the number of observations and identify the variables that are
available. Calculate a summary of each variable using the `summary`
function directly on the `data.frame`.
- Calculate the mean and the median age and consumption.
- Do men consume more beer than women on average? To answer this
question, calculate the mean consumption for men only, selecting the
observations with `Gender` equal to `Male`. Then do the same for
observations with `Gender` equal to `Female`.
- Using the `table()` function, generate a two-way table of gender and
employment status.
- Remove observations with missing values and export the data into a
new `csv` file called `beers_no_na.csv`.
`r msmbstyle::question_end()`
`r msmbstyle::question_begin()`
We are going to analyse clinical data from The Cancer Genome Atlas
(TCGA). The data are available in the `rWSBIM1207` package.
- Load the `rWSBIM1207` package. If the package isn't installed of its
version is greater than 0.1.1, install it from the
`UCLouvain-CBIO/rWSBIM1207` GitHub repository using the
`BiocManager::install()` (or `remotes::install_github()`)
functions. If you use a recent Renku enironment, the package is
already available.
- Obtain the path to the csv file containing the clinical data need for
this exercise using the `clinical1.csv` function and read it into R
as a `data.frame` called `clinical`.
- Inspect the data using `str` and `View`. How many patients are
recorded in the table?
- Print the column names using two different functions.
- Create a smaller data frame called `clinical_mini` containing only
the columns corresponding to the `patientID`, `gender`,
`age_at_diagnosis` and `smoking_history`. Try to do this using
column indices and column names.
- Inspect the `smoking_history` column. How many categories are
recorded? How many observations are there for each category?
- The column age at diagnosis is recorded in days. Create a new column
`years_at_diagnosis` corresponding to the age at diagnosis converted
in years.
- Calculate the mean and median age at diagnosis. Hint: pay attention
to missing values!
- Is there a difference between the `years_at_diagnosis` for male and
female patients?
- Use the `quantile` function to calculate the first and last quartile
of age at diagnosis. Use the help function (`?quantile`) to see how
to use the `quantile` function.
- Use the `summary` function to confirm your previous results.
`r msmbstyle::question_end()`
<!-- `r msmbstyle::solution_begin()` -->
<!-- ```{r} -->
<!-- library("rWSBIM1207") -->
<!-- clinical <- read.csv(clinical1.csv()) -->
<!-- str(clinical) -->
<!-- names(clinical) -->
<!-- ## or -->
<!-- colnames(clinical) -->
<!-- clinical_mini <- clinical[, c(1, 3, 4, 12)] -->
<!-- ## or -->
<!-- clinical_mini <- clinical[, c("patientID", "gender", "age_at_diagnosis", "smoking_history")] -->
<!-- levels(clinical_mini$smoking_history) -->
<!-- table(clinical_mini$smoking_history) -->
<!-- clinical_mini$years_at_diagnosis <- clinical_mini$age_at_diagnosis / 365 -->
<!-- mean(clinical_mini$years_at_diagnosis, na.rm = TRUE) -->
<!-- median(clinical_mini$years_at_diagnosis, na.rm = TRUE) -->
<!-- quantile(clinical_mini$years_at_diagnosis, 0.25, na.rm = TRUE) -->
<!-- quantile(clinical_mini$years_at_diagnosis, 0.75, na.rm = TRUE) -->
<!-- summary(clinical_mini$years_at_diagnosis) -->
<!-- ``` -->
<!-- `r msmbstyle::solution_end()` -->