extracting elements from a vector based on some conditions that we specify
There are four main types of index vectors in R:
Logical indexing is especially useful for filtering.
For example, x[is.na(x)] returns all the NA values in vector x.
x <- c(1, NA, 3, NA, 5)
is.na(x)
# [1] FALSE TRUE FALSE TRUE FALSE
x: 1 NA 3 NA 5
is.na(x): FALSE TRUE FALSE TRUE FALSE
x[is.na(x)]
#gives you
[1] NA NA
IMPORTANT: We can use the following to extract all non- NA values from a vector
y<- x[!is.na(x)]
Finding positive values
y>0 #gives true for the values greater than 0
y[y>0] #gives values where value is greater than 0
x[x>0] #doesnt work bcs NA>0 just gives NA
x[!is.na(x) & x>0]. #gives the values of x that are not NA, and positive
specific values
x[index] is obvious. Also R uses one based indexing, so first element has index 1
x[c(3,5,7)]
#creates a vector of numbers and passes that as an index to the vector x
x[-c(2,10)]
#the indexes 2 and 10 wont be included
Note: R will let you call indexes out of bounds, without error so be careful
named vectors
You can also subset named vectors using character indexing, where the names act like keys.
This is similar to how a hashmap or dictionary works in other languages like Python or JavaScript — you’re using a name to retrieve a value.