dropDims: Drop singleton dimensions of an array

Description Usage Arguments Examples

View source: R/array_transform.R

Description

dropDims drops singleton dimensions (whose lengths is 1) of a multidimensional array. Compared to drop and adrop this function gives more control over which singleton dimensions to drop.

Usage

1
2
3
4
5
6
7
8
dropDims(
  x,
  drop = TRUE,
  keep = NULL,
  return_array = TRUE,
  named_vector = TRUE,
  stop_if_missing = TRUE
)

Arguments

x

an array (or a matrix, or a list with dim attribute)

drop

1) either a single logical whether all singleton dimensions should be dropped (default: TRUE); or 2) a logical, integer or character vector indicating which dimensions to drop (if character, x must have named dimensions). If drop is a vector (case 2), the referred dimensions must be present in x.

keep

an integer or character vector indicating those dimensions which must remain in the returned value even if they are singletons (if character, x must have named dimensions). Note that keep has a higher priority than drop. Also note that to-be-kept dimensions which are actually not present in x are simply ignored (instead of resulting in error).

return_array

logical value whether dropDims should return a one-dimensional array instead of a vector even if all or all but one dimension of x is dropped (default: TRUE)

named_vector

logical value whether a vector result should be named (TRUE, the default). Ignored if return_array is TRUE.

stop_if_missing

logical value whether dropping or keeping non-existent dimensions should result in error (TRUE, default), or should be ignored (FALSE)

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
# create example data
x <- array(1:4, c(2, 1, 2, 1),
           dimnames = list(dimA = letters[1:2],
                           dimB = "a",
                           dimC = LETTERS[1:2],
                           dimD = "z"))

# drop all singleton dimensions
x0 <- dropDims(x)
stopifnot(identical(dim(x0), c(2L, 2L)))

# drop all singleton dimensions but always keep dimD
x1 <- dropDims(x, keep = "dimD")
stopifnot(identical(dim(x1), c(2L, 2L, 1L)))

# create a new example; a list with dim attribute
( x <- array(list(1:2, letters[1:2]), c(2, 1, 1),
             dimnames = list(type = c("numeric", "character"),
                             single1 = "a",
                             single2 = "b")) )

# drop the single1 dimension
x0 <- dropDims(x, "single1")
stopifnot(identical(dim(x0), c(2L, 1L)))

# wrong dimension in drop: by default, it results in an error
# (note that x has only 3 dimensions, not 4)
x1 <- try(dropDims(x, drop = 1:4), silent = TRUE)
stopifnot(inherits(x1, "try-error"))

# however, you can also ask for skipping those missing dimensions in 'drop'
# and 'keep'
x2 <- dropDims(x, drop = 1:4, stop_if_missing = FALSE)
stopifnot(identical(dim(x2), 2L))

# by default, dropDims returns an array, even if it has only one dimension
( x <- array(1:3, c(3, 1), list(dimA = letters[1:3], dimB = "A")) )
( x0 <- dropDims(x) )
stopifnot(is.array(x0))

# you can change this behaviour
( x1 <- dropDims(x, return_array = FALSE) )
stopifnot(!is.array(x1))

# vector results are named by default...
stopifnot(identical(names(x1), letters[1:3]))

# ...but not necessarily
( x2 <- dropDims(x, return_array = FALSE, named_vector = FALSE) )
stopifnot(is.null(names(x2)))
stopifnot(identical(unname(x1), x2))

tdeenes/eegR documentation built on April 19, 2021, 4:17 p.m.