-
Notifications
You must be signed in to change notification settings - Fork 0
/
_016_arrays.R
383 lines (306 loc) · 11.2 KB
/
_016_arrays.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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# Filename: _016_arrays.R
# Title: Arrays and its built in functions in R
# Author: Raghava | GitHub: @raghavtwenty
# Date Created: June 5, 2024 | Last Updated: June 5, 2024
# Language: R | Version: 4.4.0
# Arrays
# Creating arrays
print("Creating arrays")
thisarray <- c(1:24)
multiarray <- array(thisarray, dim = c(4, 3, 2),)
# Accessing elements of an array
print("Accessing elements of an array")
print(multiarray[2, 3, 2])
# Creating an array with dimensions and names
print("Creating an array with dimensions and names")
named_array <- array(1:12, dim = c(3, 2, 2),
dimnames = list(c("row1", "row2", "row3"),
c("col1", "col2"),
c("slice1", "slice2")))
print(named_array)
# Sum over the rows (1st dimension)
print("Sum over the rows (1st dimension)")
row_sums <- apply(multiarray, 1, sum)
print(row_sums)
# Sum over the columns (2nd dimension)
print("Sum over the columns (2nd dimension)")
col_sums <- apply(multiarray, 2, sum)
print(col_sums)
# Sum over the slices (3rd dimension)
print("Sum over the slices (3rd dimension)")
slice_sums <- apply(multiarray, 3, sum)
print(slice_sums)
# Finding dimensions of the array
print("Finding dimensions of the array")
dims <- dim(multiarray)
print(dims)
# Finding the length (number of elements) of the array
print("Finding the length (number of elements) of the array")
len <- length(multiarray)
print(len)
# Finding the transpose of a 2D slice of the array
print("Finding the transpose of a 2D slice of the array")
transpose_slice <- t(multiarray[,,1])
print(transpose_slice)
# Reshaping the array (flatten to vector)
print("Reshaping the array (flatten to vector)")
flattened <- as.vector(multiarray)
print(flattened)
# Reshaping the array to a different dimension
print("Reshaping the array to a different dimension")
reshaped <- array(flattened, dim = c(2, 12))
print(reshaped)
# Replacing values in the array
print("Replacing values in the array")
multiarray[multiarray %% 2 == 0] <- 0
print(multiarray)
# Arithmetic operations on arrays
print("Arithmetic operations on arrays")
array1 <- array(1:12, dim = c(3, 2, 2))
array2 <- array(13:24, dim = c(3, 2, 2))
# Element-wise addition
print("Element-wise addition")
array_sum <- array1 + array2
print(array_sum)
# Element-wise multiplication
print("Element-wise multiplication")
array_prod <- array1 * array2
print(array_prod)
# Slicing arrays
print("Slicing arrays")
slice1 <- multiarray[,,1]
print(slice1)
# Extracting the first row across all slices
row1_all_slices <- multiarray[1,,]
print(row1_all_slices)
# Extracting the first column across all slices
col1_all_slices <- multiarray[,1,]
print(col1_all_slices)
# Checking if an object is an array
print("Checking if an object is an array")
is_array <- is.array(multiarray)
print(is_array)
# Getting the length of each dimension
print("Getting the length of each dimension")
dim_lengths <- dim(multiarray)
print(dim_lengths)
# Getting the number of dimensions
print("Getting the number of dimensions")
num_dims <- length(dim(multiarray))
print(num_dims)
# Creating an empty array
print("Creating an empty array")
empty_array <- array(NA, dim = c(4, 3, 2))
print(empty_array)
# Filling an array with a specific value
print("Filling an array with a specific value")
filled_array <- array(5, dim = c(4, 3, 2))
print(filled_array)
# Summarizing an array
print("Summarizing an array")
summary_array <- summary(multiarray)
print(summary_array)
# Checking if an object is atomic
print("Checking if an object is atomic")
is_atomic <- is.atomic(multiarray)
print(is_atomic)
# Replicating an array
print("Replicating an array")
replicated_array <- replicate(2, multiarray)
print(replicated_array)
# Sorting an array (requires conversion to vector)
print("Sorting an array")
sorted_array <- array(sort(as.vector(multiarray)), dim = dim(multiarray))
print(sorted_array)
# Finding unique elements in an array (requires conversion to vector)
print("Finding unique elements in an array")
unique_elements <- unique(as.vector(multiarray))
print(unique_elements)
# Checking for NA values
print("Checking for NA values")
has_na <- anyNA(multiarray)
print(has_na)
# Replace NA values with a specific value
print("Replace NA values with a specific value")
multiarray_with_na <- multiarray
multiarray_with_na[1, 1, 1] <- NA
na_replaced <- ifelse(is.na(multiarray_with_na), 0, multiarray_with_na)
print(na_replaced)
# Getting the mode of the array
print("Getting the mode of the array")
mode_array <- mode(multiarray)
print(mode_array)
# Converting an array to a matrix (2D only)
print("Converting an array to a matrix (2D only)")
array_to_matrix <- as.matrix(multiarray[,,1])
print(array_to_matrix)
# Generating an array of random numbers
print("Generating an array of random numbers")
random_array <- array(rnorm(24), dim = c(4, 3, 2))
print(random_array)
# Applying a function element-wise
print("Applying a function element-wise")
element_wise_function <- array(sqrt(multiarray), dim = dim(multiarray))
print(element_wise_function)
# Setting and getting dimension names
print("Setting and getting dimension names")
dimnames(multiarray) <- list(c("R1", "R2", "R3", "R4"), c("C1", "C2", "C3"), c("S1", "S2"))
dimnames_array <- dimnames(multiarray)
print(dimnames_array)
# Creating an identity matrix array
print("Creating an identity matrix array")
identity_array <- diag(4) # Creates a 4x4 identity matrix
print(identity_array)
# Getting the names of the dimensions
print("Getting the names of the dimensions")
dimnames_array <- dimnames(multiarray)
print(dimnames_array)
# Repeating an array
print("Repeating an array")
repeated_array <- array(rep(1:6, each = 4), dim = c(4, 3, 2))
print(repeated_array)
# Combining arrays along rows
print("Combining arrays along rows")
row_combined_array <- rbind(multiarray[,,1], multiarray[,,2])
print(row_combined_array)
# Combining arrays along columns
print("Combining arrays along columns")
col_combined_array <- cbind(multiarray[,,1], multiarray[,,2])
print(col_combined_array)
# Checking if the array is numeric
print("Checking if the array is numeric")
is_numeric_array <- is.numeric(multiarray)
print(is_numeric_array)
# Checking if the array is integer
print("Checking if the array is integer")
is_integer_array <- is.integer(multiarray)
print(is_integer_array)
# Checking if the array is logical
print("Checking if the array is logical")
is_logical_array <- is.logical(multiarray)
print(is_logical_array)
# Applying a custom function using lapply (requires conversion to list)
print("Applying a custom function using lapply")
lapply_result <- lapply(as.list(multiarray), function(x) x * 2)
print(lapply_result)
# Splitting an array into a list
print("Splitting an array into a list")
split_array <- split(multiarray, f = multiarray)
print(split_array)
# Getting the row sums and column sums of 2D slices
print("Getting the row sums and column sums of 2D slices")
row_sums_slice <- rowSums(multiarray[,,1])
print(row_sums_slice)
col_sums_slice <- colSums(multiarray[,,1])
print(col_sums_slice)
# Calculating the mean across dimensions
print("Calculating the mean across dimensions")
mean_array <- apply(multiarray, c(1, 2), mean)
print(mean_array)
# Calculating the variance across dimensions
print("Calculating the variance across dimensions")
var_array <- apply(multiarray, c(1, 2), var)
print(var_array)
# Calculating the standard deviation across dimensions
print("Calculating the standard deviation across dimensions")
sd_array <- apply(multiarray, c(1, 2), sd)
print(sd_array)
# Using outer to perform outer product
print("Using outer to perform outer product")
outer_product <- outer(1:3, 1:3)
print(outer_product)
# Creating a cross-tabulation (contingency table)
print("Creating a cross-tabulation (contingency table)")
cross_tab <- table(multiarray[,,1], multiarray[,,2])
print(cross_tab)
# Creating an array
print("Creating an array")
thisarray <- c(1:24)
multiarray <- array(thisarray, dim = c(4, 3, 2))
# Getting the mode of the array
print("Getting the mode of the array")
mode_array <- mode(multiarray)
print(mode_array)
# Converting an array to a matrix (2D only)
print("Converting an array to a matrix (2D only)")
array_to_matrix <- as.matrix(multiarray[,,1])
print(array_to_matrix)
# Generating an array of random numbers
print("Generating an array of random numbers")
random_array <- array(rnorm(24), dim = c(4, 3, 2))
print(random_array)
# Applying a function element-wise
print("Applying a function element-wise")
element_wise_function <- array(sqrt(multiarray), dim = dim(multiarray))
print(element_wise_function)
# Setting and getting dimension names
print("Setting and getting dimension names")
dimnames(multiarray) <- list(c("R1", "R2", "R3", "R4"), c("C1", "C2", "C3"), c("S1", "S2"))
dimnames_array <- dimnames(multiarray)
print(dimnames_array)
# Creating an identity matrix array
print("Creating an identity matrix array")
identity_array <- diag(4) # Creates a 4x4 identity matrix
print(identity_array)
# Getting the names of the dimensions
print("Getting the names of the dimensions")
dimnames_array <- dimnames(multiarray)
print(dimnames_array)
# Repeating an array
print("Repeating an array")
repeated_array <- array(rep(1:6, each = 4), dim = c(4, 3, 2))
print(repeated_array)
# Combining arrays along rows
print("Combining arrays along rows")
row_combined_array <- rbind(multiarray[,,1], multiarray[,,2])
print(row_combined_array)
# Combining arrays along columns
print("Combining arrays along columns")
col_combined_array <- cbind(multiarray[,,1], multiarray[,,2])
print(col_combined_array)
# Checking if the array is numeric
print("Checking if the array is numeric")
is_numeric_array <- is.numeric(multiarray)
print(is_numeric_array)
# Checking if the array is integer
print("Checking if the array is integer")
is_integer_array <- is.integer(multiarray)
print(is_integer_array)
# Checking if the array is logical
print("Checking if the array is logical")
is_logical_array <- is.logical(multiarray)
print(is_logical_array)
# Applying a custom function using lapply (requires conversion to list)
print("Applying a custom function using lapply")
lapply_result <- lapply(as.list(multiarray), function(x) x * 2)
print(lapply_result)
# Splitting an array into a list
print("Splitting an array into a list")
split_array <- split(multiarray, f = multiarray)
print(split_array)
# Getting the row sums and column sums of 2D slices
print("Getting the row sums and column sums of 2D slices")
row_sums_slice <- rowSums(multiarray[,,1])
print(row_sums_slice)
col_sums_slice <- colSums(multiarray[,,1])
print(col_sums_slice)
# Calculating the mean across dimensions
print("Calculating the mean across dimensions")
mean_array <- apply(multiarray, c(1, 2), mean)
print(mean_array)
# Calculating the variance across dimensions
print("Calculating the variance across dimensions")
var_array <- apply(multiarray, c(1, 2), var)
print(var_array)
# Calculating the standard deviation across dimensions
print("Calculating the standard deviation across dimensions")
sd_array <- apply(multiarray, c(1, 2), sd)
print(sd_array)
# Using outer to perform outer product
print("Using outer to perform outer product")
outer_product <- outer(1:3, 1:3)
print(outer_product)
# Creating a cross-tabulation (contingency table)
print("Creating a cross-tabulation (contingency table)")
cross_tab <- table(multiarray[,,1], multiarray[,,2])
print(cross_tab)