@@ -5,222 +5,31 @@ library(readr)
5
5
library(forcats )
6
6
7
7
8
- # # ----numChar------------------------------------------------------------------
9
- class(c(" Andrew" , " Jaffe" ))
10
- class(c(1 , 4 , 7 ))
11
-
12
-
13
8
# # ----seq----------------------------------------------------------------------
14
- x = seq(from = 1 , to = 5 ) # seq() is a function
15
- x
16
- class(x )
17
-
18
-
19
- # # ----seqShort-----------------------------------------------------------------
20
- 1 : 5
9
+ seq(from = 1 , to = 5 )
10
+ seq(from = 1 , to = 5 , by = 0.1 )
21
11
22
12
23
- # # ----logical1 -----------------------------------------------------------------
13
+ # # ----logical0 -----------------------------------------------------------------
24
14
x = c(TRUE , FALSE , TRUE , TRUE , FALSE )
25
15
class(x )
26
- is.numeric(c(" Andrew" , " Jaffe" ))
27
- is.character(c(" Andrew" , " Jaffe" ))
28
-
29
-
30
- # # ----logical2-----------------------------------------------------------------
31
16
z = c(" TRUE" , " FALSE" , " TRUE" , " FALSE" )
32
17
class(z )
33
18
as.logical(z )
34
19
35
20
36
- # # ----logical_z----------------------------------------------------------------
37
- sum(as.logical(z ))
38
-
39
-
40
- # # ----logical_coercion---------------------------------------------------------
41
- is.numeric(c(" Andrew" , " Jaffe" ))
42
- is.character(c(" Andrew" , " Jaffe" ))
43
-
44
-
45
- # # ----logical_coercion2--------------------------------------------------------
46
- as.character(c(1 , 4 , 7 ))
47
- as.numeric(c(" Andrew" , " Jaffe" ))
21
+ # # ----logical1-----------------------------------------------------------------
22
+ is.logical(c(TRUE , FALSE ))
23
+ is.numeric(c(TRUE , FALSE ))
24
+ as.numeric(c(TRUE , FALSE ))
25
+ as.numeric(c(" 5" , " 0" , " $0 " ))
26
+ as.character(c(TRUE , FALSE ))
27
+ as.integer(c(TRUE , FALSE ))
28
+ as.logical(c(5 , 0 ))
48
29
49
30
50
31
# # ----factor1------------------------------------------------------------------
51
32
x = factor (c(" boy" , " girl" , " girl" , " boy" , " girl" ))
52
33
x
53
34
class(x )
54
35
55
-
56
- # # -----------------------------------------------------------------------------
57
- x = c(0 , 2 , 2 , 3 , 4 )
58
- (x == 0 | x == 2 )
59
-
60
-
61
- # # -----------------------------------------------------------------------------
62
- x %in% c(0 , 2 ) # NEVER has NA and returns logical
63
-
64
-
65
- # # ----factor2------------------------------------------------------------------
66
- cc = factor (c(" case" ," case" ," case" ,
67
- " control" ," control" ," control" ))
68
- cc
69
-
70
-
71
- # # -----------------------------------------------------------------------------
72
- levels(cc ) = c(" control" ," case" )
73
- cc
74
-
75
-
76
- # # ----factor_cc_again----------------------------------------------------------
77
- casecontrol = c(" case" ," case" ," case" ," control" ,
78
- " control" ," control" )
79
- factor (casecontrol , levels = c(" control" ," case" ) )
80
- factor (casecontrol , levels = c(" control" ," case" ),
81
- ordered = TRUE )
82
-
83
-
84
- # # ----factorCheck--------------------------------------------------------------
85
- cc = factor (c(" case" ," case" ," case" ,
86
- " control" ," control" ," control" ))
87
- relevel(cc , " control" )
88
-
89
-
90
- # # -----------------------------------------------------------------------------
91
- fct_relevel(cc , " control" )
92
-
93
-
94
- # # -----------------------------------------------------------------------------
95
- levels(fct_inorder(chickwts $ feed ))
96
- levels(fct_infreq(chickwts $ feed ))
97
- levels(fct_lump(chickwts $ feed , n = 1 ))
98
-
99
-
100
- # # ----factor3------------------------------------------------------------------
101
- x = factor (casecontrol ,
102
- levels = c(" control" ," case" ) )
103
- as.character(x )
104
- as.numeric(x )
105
-
106
-
107
- # # ----rep1---------------------------------------------------------------------
108
- bg = rep(c(" boy" ," girl" ),each = 50 )
109
- head(bg )
110
- bg2 = rep(c(" boy" ," girl" ),times = 50 )
111
- head(bg2 )
112
- length(bg ) == length(bg2 )
113
-
114
-
115
- # # ---- message = FALSE---------------------------------------------------------
116
- circ = jhur :: read_circulator()
117
- head(sort(circ $ date ))
118
- library(lubridate ) # great for dates!
119
- circ = mutate(circ , newDate2 = mdy(date ))
120
- head(circ $ newDate2 )
121
- range(circ $ newDate2 ) # gives you the range of the data
122
-
123
-
124
- # # ---- message = FALSE---------------------------------------------------------
125
- x = c(" 2014-02-4 05:02:00" , " 2016/09/24 14:02:00" )
126
- ymd_hms(x )
127
-
128
-
129
- # # -----------------------------------------------------------------------------
130
- ymd_hm(x )
131
-
132
-
133
- # # ---- message = FALSE---------------------------------------------------------
134
- x = c(" 2014-02-4 05:02:00" , " 2016/09/24 14:02:00" )
135
- dates = ymd_hms(x )
136
- class(dates )
137
-
138
-
139
- # # -----------------------------------------------------------------------------
140
- theTime = Sys.time()
141
- theTime
142
- class(theTime )
143
- theTime + as.period(20 , unit = " minutes" ) # the future
144
-
145
-
146
- # # -----------------------------------------------------------------------------
147
- the_future = ymd_hms(" 2020-12-31 11:59:59" )
148
- the_future - theTime
149
- difftime(the_future , theTime , units = " weeks" )
150
-
151
-
152
- # # ----matrix-------------------------------------------------------------------
153
- n = 1 : 9
154
- n
155
- mat = matrix (n , nrow = 3 )
156
- mat
157
-
158
-
159
- # # ----subset3------------------------------------------------------------------
160
- mat [1 , 1 ] # individual entry: row 1, column 1
161
- mat [1 , ] # first row
162
- mat [, 1 ] # first columns
163
-
164
-
165
- # # ----subset4------------------------------------------------------------------
166
- class(mat [1 , ])
167
- class(mat [, 1 ])
168
-
169
-
170
- # # ----makeList, comment="", prompt=TRUE----------------------------------------
171
- mylist <- list (letters = c(" A" , " b" , " c" ),
172
- numbers = 1 : 3 , matrix (1 : 25 , ncol = 5 ))
173
-
174
-
175
- # # ----Lists, comment="", prompt=TRUE-------------------------------------------
176
- head(mylist )
177
-
178
-
179
- # # ----Listsref1, comment="", prompt=TRUE---------------------------------------
180
- mylist [1 ] # returns a list
181
- mylist [" letters" ] # returns a list
182
-
183
-
184
- # # ----Listsrefvec, comment="", prompt=TRUE-------------------------------------
185
- mylist [[1 ]] # returns the vector 'letters'
186
- mylist $ letters # returns vector
187
- mylist [[" letters" ]] # returns the vector 'letters'
188
-
189
-
190
- # # ----Listsref2, comment="", prompt=TRUE---------------------------------------
191
- mylist [1 : 2 ] # returns a list
192
-
193
-
194
- # # ----Listsref3, comment="", prompt=TRUE---------------------------------------
195
- mylist $ letters [1 ]
196
- mylist [[2 ]][1 ]
197
- mylist [[3 ]][1 : 2 ,1 : 2 ]
198
-
199
-
200
- # # ---- message=FALSE-----------------------------------------------------------
201
- circ %> %
202
- mutate(first_date = first(newDate2 ),
203
- last_date = last(newDate2 ),
204
- third_date = nth(newDate2 , 3 )) %> %
205
- select(day , date , first_date , last_date , third_date ) %> % head(3 )
206
-
207
-
208
- # # ---- message=FALSE-----------------------------------------------------------
209
- circ %> %
210
- group_by(day ) %> %
211
- mutate(first_date = first(newDate2 ),
212
- last_date = last(newDate2 ),
213
- third_date = nth(newDate2 , 3 )) %> %
214
- select(day , date , first_date , last_date , third_date ) %> % head(3 )
215
-
216
-
217
- # # ---- message=FALSE-----------------------------------------------------------
218
- circ = circ %> %
219
- group_by(day ) %> %
220
- mutate(first_date = first(newDate2 ),
221
- diff_from_first = difftime( # time1 - time2
222
- time1 = newDate2 , time2 = first_date ))
223
- head(circ $ diff_from_first , 10 )
224
- units(circ $ diff_from_first ) = " days"
225
- head(circ $ diff_from_first , 10 )
226
-
0 commit comments