-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataFormats.R
242 lines (174 loc) · 3.9 KB
/
DataFormats.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
# File: DataFormats.R
# Course: R: An Introduction (with RStudio)
# DATA TYPES ###############################################
# Numeric
n1 <- 15 # Double precision by default
n1
#[1] 15
typeof(n1)
[1] "double"
n2 <- 1.5
n2
#[1] 1.5
typeof(n2)
#[1] "double"
# Character
c1 <- "c"
c1
#[1] "c"
typeof(c1)
#[1] "character"
c2 <- "a string of text"
c2
#[1] "a string of text"
typeof(c2)
#[1] "character"
# Logical
l1 <- TRUE
l1
#[1] TRUE
typeof(l1)
#[1] "logical"
l2 <- F
l2
#[1] FALSE
typeof(l2)
#[1] "logical"
# DATA STRUCTURES ##########################################
## Vector ##################################################
v1 <- c(1, 2, 3, 4, 5)
v1
#[1] 1 2 3 4 5
is.vector(v1)
#[1] TRUE
v2 <- c("a", "b", "c")
v2
#[1] "a" "b" "c"
is.vector(v2)
#[1] TRUE
v3 <- c(TRUE, TRUE, FALSE, FALSE, TRUE)
v3
#[1] TRUE TRUE FALSE FALSE TRUE
is.vector(v3)
#[1] TRUE
## Matrix ##################################################
m1 <- matrix(c(T, T, F, F, T, F), nrow = 2)
m1
# [,1] [,2] [,3]
#[1,] TRUE FALSE TRUE
#[2,] TRUE FALSE FALSE
m2 <- matrix(c("a", "b",
"c", "d"),
nrow = 2,
byrow = T)
m2
# [,1] [,2]
#[1,] "a" "b"
#[2,] "c" "d"
## Array ###################################################
# Give data, then dimemensions (rows, columns, tables)
a1 <- array(c( 1:24), c(4, 3, 2))
a1
#, , 1
# [,1] [,2] [,3]
#[1,] 1 5 9
#[2,] 2 6 10
#[3,] 3 7 11
#[4,] 4 8 12
#, , 2
# [,1] [,2] [,3]
#[1,] 13 17 21
#[2,] 14 18 22
#[3,] 15 19 23
#[4,] 16 20 24
## Data frame ##############################################
# Can combine vectors of the same length
vNumeric <- c(1, 2, 3)
vCharacter <- c("a", "b", "c")
vLogical <- c(T, F, T)
dfa <- cbind(vNumeric, vCharacter, vLogical)
dfa # Matrix of one data type
# vNumeric vCharacter vLogical
#[1,] "1" "a" "TRUE"
#[2,] "2" "b" "FALSE"
#[3,] "3" "c" "TRUE"
df <- as.data.frame(cbind(vNumeric, vCharacter, vLogical))
df # Makes a data frame with three different data types
# vNumeric vCharacter vLogical
#1 1 a TRUE
#2 2 b FALSE
#3 3 c TRUE
## List ####################################################
o1 <- c(1, 2, 3)
o2 <- c("a", "b", "c", "d")
o3 <- c(T, F, T, T, F)
list1 <- list(o1, o2, o3)
list1
#[[1]]
#[1] 1 2 3
#[[2]]
#[1] "a" "b" "c" "d"
#[[3]]
#[1] TRUE FALSE TRUE TRUE FALSE
list2 <- list(o1, o2, o3, list1) # Lists within lists!
list2
#[[1]]
#[1] 1 2 3
#[[2]]
#[1] "a" "b" "c" "d"
#[[3]]
#[1] TRUE FALSE TRUE TRUE FALSE
#[[4]]
#[[4]][[1]]
#[1] 1 2 3
#[[4]][[2]]
#[1] "a" "b" "c" "d"
#[[4]][[3]]
#[1] TRUE FALSE TRUE TRUE FALSE
# COERCING TYPES ###########################################
## Automatic coercion ######################################
# Goes to "least restrictive" data type
(coerce1 <- c(1, "b", TRUE))
#[1] "1" "b" "TRUE"
# coerce1 # Parenthese around command above make this moot
typeof(coerce1)
#[1] "character"
## Coerce numeric to integer ###############################
(coerce2 <- 5)
#[1] 5
typeof(coerce2)
#[1] "double"
(coerce3 <- as.integer(5))
#[1] 5
typeof(coerce3)
#[1] "integer"
## Coerce character to numeric #############################
(coerce4 <- c("1", "2", "3"))
#[1] "1" "2" "3"
typeof(coerce4)
#[1] "character"
(coerce5 <- as.numeric(c("1", "2", "3")))
#[1] 1 2 3
typeof(coerce5)
#[1] "double"
## Coerce matrix to data frame #############################
(coerce6 <- matrix(1:9, nrow= 3))
# [,1] [,2] [,3]
#[1,] 1 4 7
#[2,] 2 5 8
#[3,] 3 6 9
is.matrix(coerce6)
#[1] TRUE
(coerce7 <- as.data.frame(matrix(1:9, nrow= 3)))
# V1 V2 V3
#1 1 4 7
#2 2 5 8
#3 3 6 9
is.data.frame(coerce7)
#[1] TRUE
# CLEAN UP #################################################
# Clear environment
rm(list = ls())
# Clear console
cat("\014") # ctrl+L
# Clear mind :)