-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsettingRObjects.R
More file actions
328 lines (193 loc) · 5.56 KB
/
SubsettingRObjects.R
File metadata and controls
328 lines (193 loc) · 5.56 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#### Subsetting a Vector.
# Vectors are basic objects of R and can be subsetted using the [ operator
x <- c( "a", "b", "c", "d", "a" )
x
class(x)
# Extract the first element.
x[1]
# Extract the second element
x[2]
# To extract four elements of the vector
x[1:4]
# The index sequence need not be in order.
# i.e. a vector of indices can be specified.
x[c(1, 3, 5)]
# Can also pass a logical sequence to [] operator
# to extract elements of a vector that satisfy a given condition.
# For example, here we want the elements of x that come
# lexicographically after the letter “a”.
u <- x > "a"
u
x[u]
# Another way is to skip the creation of logical vector and get the
# subset vector directly with the logical expression
x[ x > "a" ]
#############################################################
#############################################################
### Subsetting Matrices
# Matrices can be subsetted in the usual way with (i,j) type indices.
# Here, we create simple 2x3 matrix with the matrix function.
x <- matrix( 1:6, nrow = 2, ncol = 3 )
x
# Access the element (2,1)
x[2, 1]
# Access the element (1, 2)
x[1, 2]
# Indices can also be missing. This behavior is used to access an entire
# row or column of a matrix
x[1, ] # Extract the first row
x[ , 2] # Extract the second column
class(x)
y <- x[ , 2]
class(y)# integer. i.e. y is a vector.
typeof(y) # integer.
z <- x[2, 2]
class(z) # integer
typeof(z) # integer
### Dropping Matrix dimensions
# By default, when a single element of a matrix is retrieved,
# it is returned as a vector of length 1 rather than a 1 x 1 matrix.
# Often, this is exactly what we want, but this behavior can be
# turned off by setting drop = FALSE.
v <- x[1,2] # Output is a vector
v
class(v) # integer
typeof(v) # integer
m <- x[1,2, drop = FALSE] # Output is a matrix format
m
class(m) # matirx
typeof(m) # integer
v1 <- x[1, ]
v1
class(v1)
typeof(v1)
m1 <- x[1, , drop = F]
m1
class(m1)
typeof(m1)
#########################################################################
#########################################################################
### Subsetting Lists
# Lists in R can be subsetted using all the three operators ( [], [[]], $).
# Each of them is used for different purposes
x <- list( foo = 1:4, bar = 0.6 )
x
class(x)
typeof(x)
a <- x[1]
a
class(a) # [1] "list"
typeof(a) # [1] "list"
b <- x[[1]] # to extract single elements from a list.
b # [1] 1 2 3 4
c <- x[["foo"]]
c
d <- x$foo
# [[ ]] operator can be used with compound indices.
# the $ operator can be used with literal names.
x <- list( "aa" = 1:4, "bb" = 0.6, "cc" = "hello" )
x
class(x)
typeof(x)
# Create a computed index for "aa"
name <- "aa"
x[[name]]
# element name doesn't exist
x$name
# element "aa" does exist
x$aa
########################################################################
########################################################################
#### Subsetting Nested Elements of a List
# The [[ ]] operator can take an integer sequence if a nested element of
# a list needs to be extracted.
# Creating a list that contains another list
x <- list( a = list( 10, 12, 14 ), b = c( 3.14, 2.81 ))
# Print x
x
# $a
# $a[[1]]
# [1] 10
#
#$a[[2]]
#[1] 12
#
#$a[[3]]
#[1] 14
#
#
#$b
#[1] 3.14 2.81
typeof(x) # list
class(x) # list
# Extract the first element of x
f <- x[1]
f
# $a
# $a[[1]]
# [1] 10
#
# $a[[2]]
# [1] 12
#
# $a[[3]]
# [1] 14
class(f) # list
typeof(f) # list
# Extract second element of x
s <- x[2]
s
#$b
#[1] 3.14 2.81
class(s) # list
typeof(s) # list
# Get the 3rd element of the first element
x[[c(1, 3)]] # 14
# OR
x[[1]][[3]]
# first element of the second element
x[[2]][[1]]
x[[c(2, 1)]]
####################################################################
####################################################################
### Extracting Multiple elements of a list
# The [] can be used to extract multiple elements from a list
x <- list( foo = 1:4, bar = 0.6, baz = "hello")
# to extract first and thrid element
x[c(1, 3)]
# x[c(1, 3)] is NOT the same as x[[c(1, 3)]].
x[[c(1, 3)]]
####################################################################
####################################################################
#### Partial Matching
# Partial matching of names is allowed with [[]] and $
x <- list( aardvark = 1:5 )
x
x$a # returns [1] 1 2 3 4 5
x[["a"]] # returns NULL
x[["a", exact = FALSE]] # returns [1] 1 2 3 4 5
####################################################################
####################################################################
#### Removing NA Values
# A common task in data analysis is removing missing values (NAs)
x <- c(1, 2, NA, 4, NA, 5)
x
bad <- is.na(x)
print(bad)
x[bad]
x[!bad]
# If there are multiple R objects and you want to take subset with no
# missing values in any of those objects
x <- c(1, 2, NA, 4, NA, 5)
y <- c("a", "b", NA, "d", NA, "f")
good <- complete.cases(x, y)
print(good)
x[good]
y[good]
# Another example
x <- c(1, 2, NA, 4, NA, 5)
y <- c("a", "b", NA, "d", NA, NA)
good <- complete.cases(x, y) # FALSE, if any of the combining element is NA
print(good)
x[good]
y[good]