which                  package:base                  R Documentation

_W_h_i_c_h _i_n_d_i_c_e_s _a_r_e _T_R_U_E?

_D_e_s_c_r_i_p_t_i_o_n:

     Give the 'TRUE' indices of a logical object, allowing for array
     indices.

_U_s_a_g_e:

     which(x, arr.ind = FALSE)

_A_r_g_u_m_e_n_t_s:

       x: a 'logical' vector or array.  'NA's are allowed and omitted
          (treated as if 'FALSE').

 arr.ind: logical; should *arr*ay *ind*ices be returned when 'x' is an
          array?

_V_a_l_u_e:

     If 'arr.ind == FALSE' (the default), an integer vector with
     'length' equal to 'sum(x)', i.e., to the number of 'TRUE's in 'x';
     Basically, the result is '(1:length(x))[x]'.

     If 'arr.ind == TRUE' and 'x' is an 'array' (has a 'dim'
     attribute), the result is a matrix whose rows each are the indices
     of one element of 'x'; see Examples below.

_A_u_t_h_o_r(_s):

     Werner Stahel and Peter Holzer holzer@stat.math.ethz.ch, for the
     array case.

_S_e_e _A_l_s_o:

     'Logic', 'which.min' for the index of the minimum or maximum, and
     'match' for the first index of an element in a vector, i.e., for a
     scalar 'a', 'match(a,x)' is equivalent to  'min(which(x == a))'
     but much more efficient.

_E_x_a_m_p_l_e_s:

     which(LETTERS == "R")
     which(ll <- c(TRUE,FALSE,TRUE,NA,FALSE,FALSE,TRUE))#> 1 3 7
     names(ll) <- letters[seq(ll)]
     which(ll)
     which((1:12)%%2 == 0) # which are even?
     which(1:10 > 3, arr.ind=TRUE)

     ( m <- matrix(1:12,3,4) )
     which(m %% 3 == 0)
     which(m %% 3 == 0, arr.ind=TRUE)
     rownames(m) <- paste("Case",1:3, sep="_")
     which(m %% 5 == 0, arr.ind=TRUE)

     dim(m) <- c(2,2,3); m
     which(m %% 3 == 0, arr.ind=FALSE)
     which(m %% 3 == 0, arr.ind=TRUE)

     vm <- c(m)
     dim(vm) <- length(vm) #-- funny thing with  length(dim(...)) == 1
     which(vm %% 3 == 0, arr.ind=TRUE)

