- subscripting with positive and negative indices
- character subscripting (vectors and lists)
- logical subscripting
subsetting
- creating a list for examples
myl <- list(one=10,two=20,three=30) myi <- "one"
one
-
subsetting is carried out with three different operators:
- dollar: $
- square bracket: [
- double square bracket: [[
- these are generic functions
-
there are four basic types of subscript indices
- positive integers
- negative integers
- logical vectors
- character vectors
- the first (left-most) index moves the fastest, the right-most index is the slowest (so a matrix is filled column by column)
the square bracket
- the returned value is of the same type of the value it is applied to
myl[c(1,2)]
10 20
- evaluate its second argument
myl[myi]
10
- does not use partial matching when extracting named elements
myl["on"]
- cannot be used for subscripting environments
the double square bracket
- extract a single value
myl[[1]]
10
- evaluate its second argument
myl[[myi]]
10
- does not use partial matching when extracting named elements
myl[["on"]]
the dollar
- extract a single value
myl$two
20
- does not evaluate its second argument
myl$myi
- uses partial matching extracting named elements
myl$on
10
- 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)]
[1] 2 4 7 [1] 2 4 7
- if the subscript is larger than the length of the vector, NAs are produce
myv[c(1,11,3)]
[1] 1 NA 3
- NA as subscript produces also a NA
myv[c(1,11,NA)]
[1] 1 NA NA
- zero as subscript will be ignored
myv[c(1,2,0,3)]
[1] 1 2 3
- if the subscript vector has the length zero, the result is also of length zero
myi <- numeric() myv[myi]
integer(0)
- 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
myv[-c(1,2,0,3)]
[1] 4 5 6 7 8 9 10
- NAs are not allowed
myv[-c(1,NA,3)]
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]
integer(0)
- positive and negatives subscripts cannot be mixed
to be continued... (most parts taken from Gentleman, R Programming for Bioinformatics) …
No comments :
Post a Comment