-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-recon.R
250 lines (185 loc) · 5.31 KB
/
01-recon.R
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
# topic -------------------------------------------------------------------
#learn R
#https://www.reconlearn.org/post/practical-intror.html
# atomic vectors ----------------------------------------------------------
vector_double <- c(1, 2, 3, 4)
vector_logic <- c(TRUE, FALSE, FALSE, TRUE)
vector_character <- c("A", "B", "C", "D")
vector_integer <- c(1L, 2L, 3L, 4L)
#diferencia
class(vector_double)
typeof(vector_double)
#equivalencia
class(vector_logic)
typeof(vector_logic)
class(vector_character)
typeof(vector_character)
class(vector_integer)
typeof(vector_integer)
# matrix ------------------------------------------------------------------
# 2 dim vectors
# same type of vectors
#filled by column
matrix_of_doub <- matrix(data = vector_double,
nrow = 2,
ncol = 2)
matrix_of_doub
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
dim(matrix_of_doub)
## [1] 2 2
#if more dimensions than vector,
#then it will replicate the vector
# that is coercion
matrix_of_log <- matrix(data = vector_logic,
nrow = 4,
ncol = 3)
matrix_of_log
matrix_of_char <- matrix(data = vector_character,
nrow = 4,
ncol = 4)
matrix_of_char
matrix_of_int <- matrix(data = vector_integer,
nrow = 4,
ncol = 5)
matrix_of_int
# array -------------------------------------------------------------------
# n dim vectors
# special type of matrix
# dim arguments
# 1- number of rows
# 2- number of columns
# 3- number of dimension
vector_example <-1:18
array_example <- array(data = vector_example,
dim = c(2, 3, 3))
dim(array_example)
## [1] 2 3 3
array_example
# dataframe ---------------------------------------------------------------
# similar to matrix,
# but with diferent type of vectors
# heterogeneous structure
data_example <- data.frame(vector_character,
vector_double,
vector_logic,
vector_integer)
data_example
str(data_example)
#to access: data[row,col]
data_example[1,2]
data_example[2,1]
# list --------------------------------------------------------------------
# the most complex structure
# mix of any type of object
list_example <- list(vector_character,
matrix_of_doub,
data_example)
list_example
#access ot any component
list_example[2]
list_example[[1]]
#note the class
class(list_example[2])
class(list_example[[2]])
# function ----------------------------------------------------------------
myfun01 <- function(weight,height) {
bmi <- weight/(height^2)
return(bmi)
}
myfun01(70,1.79)
myfun01(height = 1.79,weight = 70)
#components of a function
formals(myfun01)
body(myfun01)
environment(myfun01)
#add a defult argument
myfun02 <- function(weight,height,bmi_units="kg/m2") {
bmi <- weight/(height^2)
out <- paste(round(bmi,1), bmi_units)
return(out)
}
myfun02(70,1.79)
myfun02(70*100,179,bmi_units = "g/cm2")
# packages ----------------------------------------------------------------
lapply(.libPaths(), dir)
browseVignettes(package = "incidence")
# environments ------------------------------------------------------------
#carefull!
#if objects where not called inside the function
#it will search for them on the general environment
myfun03 <- function() {
z = x + y
return(z)
}
x <- 1
y <- 3
myfun03()
#even if arguments are partially defined
myfun04 <- function(xx) {
zz = xx + yy
return(zz)
}
yy <- 3
myfun04(xx = 4)
#be carefull,
#this is good to know
#but strongly not recomended
# probability distributions -----------------------------------------------
# work more on distributions
x_ <- seq(-1,1,by = 0.05)
y_ <- dnorm(x = x_,
mean = 0,sd = 1)
plot(x_,y_)
# tidyverse ---------------------------------------------------------------
library(tidyverse)
library(readxl)
#library(here)
# _ import dataset --------------------------------------------------------
dat <- read_xlsx("data-raw/PHM-EVD-linelist-2017-10-27.xlsx")
# _ explore with tidyverse ------------------------------------------------
glimpse(dat)
dat %>% arrange(age)
dat %>%
rename(fecha_inicio_sintoma=onset)
#count number of subjects per group
dat %>%
group_by(sex) %>%
summarise(number = n())
dat %>%
select(starts_with("on"))
dat %>%
slice(7:10)
dat %>%
filter(sex=="female", age<=30)
dat %>%
filter(sex=="female" & age<=30)
dat %>%
filter(sex=="female" | age<=30)
# _ explore data from package ---------------------------------------------
measles_dat <- outbreaks::measles_hagelloch_1861 %>%
as_tibble()
measles_dat
measles_dat %>% glimpse()
# _ use tidyr -------------------------------------------------------------
#cambiar ejemplo:
# entrar al contexto de tidy data
# mix between wide and long data
malaria <- tibble(
name = letters[1:10],
age = round(rnorm(10, 30, 10), 0),
gender = rep(c('f', 'm'), 5),
infection = rep(c('falciparum', 'vivax', 'vivax', 'vivax', 'vivax'), 2)
)
glimpse(malaria)
malaria %>%
spread(key = infection,value = gender)
measles_dat %>%
naniar::miss_var_summary()
measles_dat_group <- measles_dat %>%
filter(!is.na(gender)) %>%
group_by(date_of_rash,gender) %>%
summarise(cases=n()) %>%
ungroup()
measles_dat_group