forked from tpemartin/110-1-r4ds-main
-
Notifications
You must be signed in to change notification settings - Fork 0
/
week10.rmd
275 lines (234 loc) · 5.67 KB
/
week10.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
---
title: "week10"
output: html_notebook
---
# 4.2.3 Date/Time
```{r}
install.packages("lubridate")
```
```{r}
lubridate::ymd(c("2021/10/30", "2021-10-30", "2021 October 30", "2021 Oct. 30"))
lubridate::ydm(c("2021/30/10", "2021-30-10", "2021 30 October", "2021 30 Oct."))
lubridate::mdy(c("10/30/2021", "10-30-2021", "October 30, 2021", "Oct. 30, 2021"))
lubridate::dmy(c("30/10/2021", "30-10-2021", "30 October, 2021", "30 Oct., 2021"))
```
```{r}
dayInfo <- list()
dayInfo <- c("2021/10/30", "2021-10-30", "2021 October 30", "2021 Oct. 30")
class(dayInfo$ymd)
lubridate::ymd(dayInfo$ymd) -> dayInfo$ymd_parsed
```
```{r}
commonClasses <- list()
# save three different atomic vectors
commonClasses$character <- c("John", "Mary", "Bill")
commonClasses$numeraic <- c(2, 3, 4, 7)
commonClasses$logical <- c(TRUE, T, F, FALSE)
```
```{r}
# A parsed date data has Date class.
commonClasses$date <- lubridate::dmy(c("30/10/2021", "30-10-2021", "30 October, 2021", "30 Oct., 2021"))
class(commonClasses$date)
```
"2021/10/30 13:22:52"
* day style: ymd
* time style: hms
```{r}
lubridate::ymd_hms(
"2021/10/30 13:22:52",
tz="Asia/Taipei"
) -> dateTime_taipei
dateTime_taipei
```
```{r}
dateTime_taipei <-
lubridate::ymd_hms(
c("2021/10/30 13:22:52", "2021-10-31 1:22:52PM"),
tz = "Asia/Taipei")
dateTime_taipei
```
```{r}
dateTime_london <-
lubridate::mdy_hm(
c("October 30, 2021, 23:10", "Oct. 30, 2021 11:10PM"),
tz="Europe/London")
dateTime_london
```
```{r}
c(
dateTime_taipei, # name call on dateTime_taipei
dateTime_london # name call on dateTime_london
) # can form an atomic vector since they share the same class.
```
```{r}
commonClasses$date_time <- c(
dateTime_taipei,
dateTime_london
)
class(commonClasses$date_time)
```
```{r}
dateTime_london # name call on dateTime_london
dateTime_london_atTaipei <-
lubridate::with_tz(
dateTime_london, # must be a date/time class
tz="Asia/Taipei"
)
dateTime_london
dateTime_london_atTaipei
```
```{r}
lubridate::with_tz(
c("October 30, 2021, 23:10", "Oct. 30, 2021 1:10AM"),
tz="Asia/Taipei"
)
lubridate::with_tz(
c("John", "Mary"),
tz="Asia/Taipei"
)
```
# 4.2.4 Date frame
```{r}
survey_fbf <-
list(
age=c(54, 32, 28, 20), # age
gender=c("male", "female", "female", "male"), # gender
residence=c("north", "south", "east", "east"), # residence
income=c(100, 25, NA, 77) # income
)
```
```{r}
data.frame(
data_list # atomic feature vectors and named with same length
)
```
```{r}
survey_df <-
data.frame(
survey_fbf # name call on survey_fbf
)
```
```{r}
class(survey_fbf)
class(survey_df)
```
```{r}
survey_df <-
data.frame(
age=c(54, 32, 28, 20), # age
gender=c("male", "female", "female", "male"), # gender
residence=c("north", "south", "east", "east"), # residence
income=c(100, 25, NA, 77) # income
)
```
```{r}
commonClasses <- list()
commonClasses$data_frame <- survey_df
```
***
feature vectors not all atomic vectors--some are list
```{r}
survey_fbf2 <-
list(
age=list(54, 32, 28, 20), # age
gender=list("male", "female", "female", "male"), # gender
residence=list("north", "south", "east", "east"), # residence
income=list(100, 25, NA, 77) # income
)
```
```{r}
data.frame(survey_fbf2) #the same as
data.frame(
list(
age=list(54, 32, 28, 20), # age
gender=list("male", "female", "female", "male"), # gender
residence=list("north", "south", "east", "east"), # residence
income=list(100, 25, NA, 77) # income
)
)
```
```{r}
df_survey_fbf2 <- list2DF(survey_fbf2)
df_survey_fbf2
```
data frame is an extended class from list. It inherits all the features of list.
```{r}
survey_fbf$age
df_survey_fbf <- data.frame(survey_fbf)
df_survey_fbf$age
```
- inheritance feature from Object-Oridnted Programming (OOP)
```{r}
# list
survey_fbf2$age
# data frame
df_survey_fbf2$age
```
```{r}
df_survey_fbf
df_survey_fbf2
```
not only inheris methods of list (what list can do), but also has more methods (but also can do more)
```{r}
[.row, .col] # is an extension from [] extraction
```
```{r}
commonClasses$data_frame
commonClasses$data_frame[2, "age"]
commonClasses$data_frame[2, 1]
commonClasses$data_frame[c(1,4), c("income","age")]
commonClasses$data_frame[c(1,4), c(4, 1)]
```
*[], [.ral, .col] will return value with a structure as the source.
```{r}
# Remove
commonClasses$data_frame
commonClasses$data_frame[, -c(2)]
# Replace
commonClasses$data_frame
commonClasses$data_frame[2, c( "age","income")] <- data.frame(
age=c(31),
income=c(22)
)
## == commonClasses$data_frame[2, c( "age","income")] <- data.frame(
## 31,
## 22
## )
commonClasses$data_frame[c(1,2), c("age", "income")]
commonClasses$data_frame[c(1,2), c("age", "income")] <- data.frame(
age=c(10, 15),
income=c(10, 15)
)
commonClasses$data_frame[c(1,2), c("age", "income")]
# Add
commonClasses$data_frame[, "isStudent"] <- data.frame(isStudent=c(T, T, F, T))
## == commonClasses$data_frame[, "isStudent"] <- data.frame(c(T, T, F, T))
## == commonClasses$data_frame[, "isStudent"] <- c(T, T, F, T)
```
```{r}
# a list
survey_fbf[2, c(1, 3)]
# a data frame
survey_df[2, c(1, 3)]
```
# 4.2.5 Matrix
```{r}
commonClasses$matrix <- matrix(
c(2, 11, -1, 3, 4, -5),
nrow=2,
byrow = T # default is by column
)
commonClasses$matrix
class(commonClasses$matrix)
```
```{r}
# non atomic matrix
matrix_nonAtomic <- matrix(
list(
32, "John",
33, "Jane",
34, "Ben"
), nrow=2
)
matrix_nonAtomic
```