-
elements corresponding to a TRUE in the subscript vector are selected, those corresponding to a FALSE are excluded, NAs in the the logical subscript vector produce NAs
my.vector <- letters[1:10] # vector of a...j
my.vector
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
my.index <- rep(c(TRUE,FALSE),5) # the subscript vector (of length 10)
my.index
[1] TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE
my.vector[my.index]
[1] "a" "c" "e" "g" "i"
-
if the subscript vector consists of less elements than the subscripted vector, it is repeated as many times as necessary (warning if the the vector length is not a multiple of the length of the subscript vector)
short.index <- c(T,F) # T is the same as TRUE, F as FALSE
short.index
[1] TRUE FALSE
my.vector[short.index] # has the same result as above
[1] "a" "c" "e" "g" "i"
-
if the subscript vector is longer than the vector for every TRUE or NA entry without target a NA is produced
short.vector <- letters[1:3]
short.vector
[1] "a" "b" "c"
my.index <- rep(c(T,F,NA),2)
my.index
[1] TRUE FALSE NA TRUE FALSE NA
short.vector[my.index]
[1] "a" NA NA NA
No comments :
Post a Comment