subscripting with positive and negative indices
character subscripting (vectors and lists)
creating a list for examples
myl <- list(one=10,two=20,three=30)
myi <- "one"
subsetting is carried out with three different operators:
double square bracket: [[
these are generic functions
there are four basic types of subscript indices
the first (left-most) index moves the fastest, the right-most index is the slowest (so a matrix is filled column by column)
the returned value is of the same type of the value it is applied to
evaluate its second argument
does not use partial matching when extracting named elements
cannot be used for subscripting environments
the double square bracket
evaluate its second argument
does not use partial matching when extracting named elements
does not evaluate its second argument
uses partial matching extracting named elements
cannot be used for subscripting atomic vectors
subsetting with positive indices
vector of positive integer values to indicate the set to be extracted
myv <- 1:10
myi <- c(2,4,7)
myv[myi]
myv[c(2,4,7)]
if the subscript is larger than the length of the vector, NAs are produce
NA as subscript produces also a NA
zero as subscript will be ignored
if the subscript vector has the length zero, the result is also of length zero
myi <- numeric()
myv[myi]
example: select elements which have even numbered subscripts
v <- rnorm(20)
myi <- seq(2,length(v),by=2)
myi
v[myi]
[1] 2 4 6 8 10 12 14 16 18 20
[1] 0.35380115 -0.02156840 -1.51804278 0.38278037 0.03867578 -1.25803279
[7] 0.62863255 0.07111270 -0.73416837 0.18966622
subsetting with negative indices
subscript vector consists of negative integer values indicating the indices not to be extracted
zero as subscript will be ignored
Error in myv[-c(1, NA, 3)] :
nur Nullen dürfen mit negativen Indizes gemischt werden
zero length subscript produces zero length result
myv <- 1:10
myi <- numeric()
myv[-myi]
positive and negatives subscripts cannot be mixed
to be continued... (most parts taken from Gentleman, R Programming for Bioinformatics) …