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 array indices 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 who's 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.

_E_x_a_m_p_l_e_s:

     which(LETTERS == "R")
     which(ll <- c(T,F,T,NA,F,F,T))#> 1 3 7
     names(ll) <- letters[seq(ll)]
     which(ll)
     which((1:12)%%2 == 0) # which are even?
     str(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)

