Subsetting vectors
If we want to display only selected values from the object, R can help us do that easily.
For example, if we want to see the cost of the last 3 items in our food list, we would type:
cost[3:5]
Note here, that we could also type cost[c(3,4,5)]
and get the same result.
The :
operator helps us condense the code and get consecutive values.
This exercise is part of the course
Data Science R Basics
Exercise instructions
We will learn to subset using several special operators.
- Use the
[
and:
operators to access the temperature of the first three cities in the list, which are already stored intemp
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# cost of the last 3 items in our food list:
cost[3:5]
# temperatures of the first three cities in the list: