1. Learn
  2. /
  3. Courses
  4. /
  5. Data Science R Basics

Exercise

Connecting Numeric and Character Vectors

We have successfully assigned the temperatures as numeric values to temp and the city names as character values to city. But can we associate the temperature to its related city? Yes! We can do so using a code we already know - names. We assign names to the numeric values.

It would look like this:

cost <- c(50, 75, 90, 100, 150)
food <- c("pizza", "burgers", "salads", "cheese", "pasta")
names(cost) <- food

Instructions

100 XP

Use the names function and the objects defined in the previous exercises to associate the temperature data with its corresponding city. (You can go back to the previous questions and copy the objects stored.) Note: to see what happened, after assigning the city names to the temp vector, try printing the temp vector to understand how the names are associated with elements of temp.