applyAgainst: apply a function against a dimension

Description Usage Arguments Details Value Examples

View source: R/cube-transforms.R

Description

Similar to other apply functions, this takes an array and applies the function against the dimensions (specified in the MARGIN argument). These dimensions must be a single number (unlike many apply functions). See the examples below, where we use the function add_one_hundred to add 100 on to the end of each MARGIN.

Usage

1
applyAgainst(X, MARGIN, FUN, ...)

Arguments

X

an array

MARGIN

the dimension to apply the function against

FUN

the function to be applied

...

optional arguments to 'FUN“

Details

FUN can be any function that takes a vector and returns a vector, but one common use case is a function that adds new entries to the vector, effectively expanding the array in the dimension given.

Value

an array with the function applied

Examples

 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
array <- array(c(1:24), dim = c(4, 3, 2))
array
# , , 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

add_one_hundred <- function(x) c(x, 100)

crunch:::applyAgainst(array, 1, add_one_hundred)
# , , 1
#
#      [,1] [,2] [,3]
# [1,]    1    5    9
# [2,]    2    6   10
# [3,]    3    7   11
# [4,]    4    8   12
# [5,]  100  100  100
#
# , , 2
#
#      [,1] [,2] [,3]
# [1,]   13   17   21
# [2,]   14   18   22
# [3,]   15   19   23
# [4,]   16   20   24
# [5,]  100  100  100

crunch:::applyAgainst(array, 2, add_one_hundred)
# , , 1
#
#      [,1] [,2] [,3] [,4]
# [1,]    1    5    9  100
# [2,]    2    6   10  100
# [3,]    3    7   11  100
# [4,]    4    8   12  100
#
# , , 2
#
#      [,1] [,2] [,3] [,4]
# [1,]   13   17   21  100
# [2,]   14   18   22  100
# [3,]   15   19   23  100
# [4,]   16   20   24  100

crunch:::applyAgainst(array, 3, add_one_hundred)
# , , 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
#
# , , 3
#
#      [,1] [,2] [,3]
# [1,]  100  100  100
# [2,]  100  100  100
# [3,]  100  100  100
# [4,]  100  100  100

crunch documentation built on April 6, 2021, 1:05 a.m.