Sunday, August 3, 2014

R which Function


which(x, arr.ind = FALSE, useNames = TRUE)
arrayInd(ind, .dim, .dimnames = NULL, useNames = FALSE)

x: logical vector or array. NAs are allowed and omitted (treated as if FALSE)
arr.ind: logical; should array indices be returned when x is an array?
ind: integer-valued index vector, as resulting from which(x)
.dim: integer vector
.dimnames: optional list of character dimnames(.), of which only .dimnames[[1]] is used
useNames: logical indicating if the value of arrayInd() should have (non-null) dimnames at all




> BOD
  Time demand
1    1    8.3
2    2   10.3
3    3   19.0
4    4   16.0
5    5   15.6
6    7   19.8

> which(BOD$demand == 16)
[1] 4

> x <- matrix(1:9,3,3)
> x
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9

> which(x %% 3 == 0, arr.ind=TRUE)
     row col
[1,]   3   1
[2,]   3   2
[3,]   3   3

> which(x %% 3 == 0, arr.ind=FALSE)
[1] 3 6 9



No comments:

Post a Comment