Читать книгу The Big R-Book - Philippe J. S. De Brouwer - Страница 55
4.3.3 Accessing Data from a Vector
ОглавлениеIn many situations you will need to address a vector as a whole, but there will also be many occasions where it is necessary to do something with just one element. Since this is such a common situation, R provides more than one way to get this done.
# Create v as a vector of the numbers one to 5: v <- c(1:5) # Access elements via indexing: v[2] ## [1] 2 v[c(1,5)] ## [1] 1 5 # Access via TRUE/FALSE: v[c(TRUE,TRUE,FALSE,FALSE,TRUE)] ## [1] 1 2 5 # Access elements via names: v <- c("pear" = "green", "banana" = "yellow", "coconut" = "brown") v ## pear banana coconut ## "green" "yellow" "brown" v["banana"] ## banana ## "yellow" # Leave out certain elements: v[c(-2,-3)] ## pear ## "green"