Repository navigation
Expand file tree
/
Copy pathweek3.R
More file actions
237 lines (138 loc) · 8.09 KB
/
Copy pathweek3.R
File metadata and controls
237 lines (138 loc) · 8.09 KB
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
## ----eval = FALSE---------------------------------------------------------------
# library(tibble)
## -------------------------------------------------------------------------------
library(tidyverse)
## ----eval = FALSE---------------------------------------------------------------
# install.packages("tidyverse")
## -------------------------------------------------------------------------------
beatles <- tibble(
name = c("John", "Paul", "Ringo", "George"),
birth_year = c(1940, 1942, 1940, 1943),
instrument = c("guitar", "bass", "drums", "guitar")
)
beatles
## -------------------------------------------------------------------------------
iris_tibble <- as_tibble(iris)
## -------------------------------------------------------------------------------
class(iris)
class(iris_tibble)
## ----results=TRUE---------------------------------------------------------------
## inspect the data
str(iris_tibble)
## ----eval=FALSE, purl=TRUE------------------------------------------------------
# download.file(url="https://bioinformatics-core-shared-training.github.io/Bitesize-R/data/brca_metabric_clinical_data.tsv",destfile = "data/brca_metabric_clinical_data.tsv")
# download.file(url="https://bioinformatics-core-shared-training.github.io/Bitesize-R/data/metabric_clinical_and_expression_data.csv",destfile = "data/metabric_clinical_and_expression_data.csv")
## -------------------------------------------------------------------------------
metabric <- read_tsv("data/brca_metabric_clinical_data.tsv")
## -------------------------------------------------------------------------------
metabric
## **`read_csv()`** and **`read_tsv()`**
##
## `read_csv()` reads data from a comma-separated value (CSV) file into a tibble.
## `read_tsv()` is the equivalent function that works on tab-delmited files.
##
## These functions expect the first line to contain column names and try to make
## sensible guesses at the type of data in each column. You can change this by
## specifying various arguments, e.g. to skip comment lines beginning with a
## specific character (e.g. "#") or to tell the function what the column types
## are.
##
## -------------------------------------------------------------------------------
#install.packages("janitor")
library(janitor)
metabric <- clean_names(metabric)
## -------------------------------------------------------------------------------
table(metabric$chemotherapy)
## -------------------------------------------------------------------------------
summary(metabric$mutation_count)
## ----eval = FALSE---------------------------------------------------------------
# getwd()
## -------------------------------------------------------------------------------
metabric$mutation_count[1:10]
## **Missing values** (**`NA`**)
##
## Missing values in R are represented as **`NA`**, which stands for 'not available'.
## -------------------------------------------------------------------------------
mean(metabric$mutation_count)
## -------------------------------------------------------------------------------
mean(metabric$mutation_count, na.rm = TRUE)
## -------------------------------------------------------------------------------
first_ten_mutation_counts <- metabric$mutation_count[1:10]
is.na(first_ten_mutation_counts)
## -------------------------------------------------------------------------------
sum(is.na(metabric$mutation_count))
## -------------------------------------------------------------------------------
library(tidyverse)
## ----message = FALSE------------------------------------------------------------
metabric <- read_csv("data/metabric_clinical_and_expression_data.csv")
metabric
## -------------------------------------------------------------------------------
deceased <- filter(metabric, Vital_status == "Died of Disease")
deceased
## ----eval = FALSE---------------------------------------------------------------
# filter(metabric, Vital_status == "Died of Disease")
# # is equivalent to
# metabric[metabric$Vital_status == "Died of Disease", ]
## -------------------------------------------------------------------------------
table(metabric$Vital_status)
## -------------------------------------------------------------------------------
deceased <- filter(metabric, Vital_status != "Living")
nrow(deceased)
## -------------------------------------------------------------------------------
deceased <- filter(metabric, Vital_status %in% c("Died of Disease", "Died of Other Causes"))
nrow(deceased)
## -------------------------------------------------------------------------------
deceased <- filter(metabric, str_starts(Vital_status, "Died"))
nrow(deceased)
## -------------------------------------------------------------------------------
# create a new logical variable called 'Deceased'
metabric$Deceased <- metabric$Survival_status == "DECEASED"
#
# filtering based on a logical variable - only selects TRUE values
deceased <- filter(metabric, Deceased)
#
# only display those columns we're interested in
deceased[, c("Patient_ID", "Survival_status", "Vital_status", "Deceased")]
## ----eval = FALSE---------------------------------------------------------------
# filter(metabric, !Deceased)
## -------------------------------------------------------------------------------
missing_vital_status <- filter(metabric, is.na(Vital_status))
missing_vital_status[, c("Patient_ID", "Survival_status", "Vital_status", "Deceased")]
## -------------------------------------------------------------------------------
filter(metabric, Survival_status == "LIVING", Survival_time > 120)
## ----eval = FALSE---------------------------------------------------------------
# metabric[metabric$Survival_status == "LIVING" & metabric$Survival_time > 120, ]
## ----eval = FALSE---------------------------------------------------------------
# filter(metabric, Survival_status == "LIVING", Survival_time > 120)
## -------------------------------------------------------------------------------
selected_patients <- filter(metabric, Tumour_stage >= 3, ER_status == "Positive" | PR_status == "Positive")
nrow(selected_patients)
## ----eval = FALSE---------------------------------------------------------------
# filter(metabric, Tumour_stage >= 3 & (ER_status == "Positive" | PR_status == "Positive"))
## -------------------------------------------------------------------------------
deceased[, c("Patient_ID", "Survival_status", "Vital_status", "Deceased")]
## -------------------------------------------------------------------------------
select(metabric, Patient_ID, Survival_status, Vital_status, Deceased)
## -------------------------------------------------------------------------------
select(metabric, Patient_ID, Vital_status, Survival_status, Deceased)
## -------------------------------------------------------------------------------
select(metabric, Patient_ID, Chemotherapy:Tumour_stage)
## -------------------------------------------------------------------------------
select(metabric, contains("status"))
## -------------------------------------------------------------------------------
select(metabric, ends_with("status"))
## -------------------------------------------------------------------------------
select(metabric, -Cohort)
## -------------------------------------------------------------------------------
selected_columns <- select(metabric, Patient_ID, starts_with("Tumour_"), `3-gene_classifier`:Integrative_cluster, -Cellularity)
selected_columns
## -------------------------------------------------------------------------------
select(metabric, Patient_ID, Survival_status, Tumour_stage, everything())
## -------------------------------------------------------------------------------
select(metabric, Patient_ID, Classification = `3-gene_classifier`, NPI = Nottingham_prognostic_index)
## -------------------------------------------------------------------------------
arrange(metabric, Survival_time)
## -------------------------------------------------------------------------------
arrange(metabric, desc(Survival_time))
## -------------------------------------------------------------------------------
arrange(metabric, Tumour_stage, Nottingham_prognostic_index)