unwhich: Gets a logical vector/matrix from indices

Description Usage Arguments Author(s) See Also Examples

Description

Gets a logical vector/matrix from indices, indices which indicates elements that are TRUE. All other elements are FALSE.

If the given set of indices is a integer vector, the result will be a logical vector. The length of the vector is as long as the length of the input vector, but it can also be specified using the argument dim.

If the given set of indices is a matrix with two columns (as returned by which(x, arr.ind=TRUE) the result is a logical matrix. The dimension of the minimum size such that all indices in the input matrix exists, but it can also be specified using the argument dim, where dim[1] is the number of rows and dim[2] is the number of columns.

Usage

1
2
## Default S3 method:
unwhich(x, dim=NULL, ...)

Arguments

x

The index vector/matrix.

dim

For a index vector this is a singular value and for a index matrix it is a integer vector of length two. If NULL the minimum dimension will used.

...

Not used.

Author(s)

Henrik Bengtsson

See Also

which().

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
  # Examples with vectors
  vec <- rep(c(TRUE, TRUE, FALSE), 3)

  idx <- which(vec)
  print(idx)    # 1 2 4 5 7 8

  log <- unwhich(idx)
  print(log)    # TRUE TRUE FALSE TRUE TRUE FALSE TRUE TRUE

  log <- unwhich(idx, length(vec))
  print(log)    # TRUE TRUE FALSE TRUE TRUE FALSE TRUE TRUE FALSE

  # Examples with matrices
  mat <- matrix(c(TRUE, TRUE, FALSE), nrow=3, ncol=3)

  idx <- which(mat, arr.ind=TRUE)
  print(idx)
  # 	   row col
  # [1,]   1   1
  # [2,]   2   1
  # [3,]   1   2
  # [4,]   2   2
  # [5,]   1   3
  # [6,]   2   3

  log <- unwhich(idx)
  print(log)
  #      [,1] [,2] [,3]
  # [1,] TRUE TRUE TRUE
  # [2,] TRUE TRUE TRUE

  log <- unwhich(idx, dim=dim(mat))
  print(log)
  #       [,1]  [,2]  [,3]
  # [1,]  TRUE  TRUE  TRUE
  # [2,]  TRUE  TRUE  TRUE
  # [3,] FALSE FALSE FALSE

HenrikBengtsson/R.basic documentation built on May 6, 2019, 11:51 p.m.