Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction to data frames
Data frame structure
Accessing columns in a data frame
12. Accessing several columns with indexing
Accessing rows and columns combined in a data frame
Data frame analysis
Summary

Instruction

That's correct! Of course, as you may have guessed, we can retrieve more than one column in single pass. For example, if you want to retrieve city names and their corresponding country names from the cities data frame in one go, you can do so like this:

cities[, c(1, 2)]

Inside the brackets, we simply specify both column indexes (1 is the index of the country column, and 2 is the index of the city column) with the help of the c() function. Of course, you can list as many valid indexes as you'd like.

The result of cities[, c(1, 2)] is also a data frame itself.

Exercise

Versico would like to know the names of the regions in which their candidate European countries are located. After all, they don't just want to expand their business in one region.

Display the country names with their corresponding region names, using the countries data frame. Recall that country is the first column and region is the second column of the countries data frame.

Stuck? Here's a hint!

Make a call to the c() function, passing in the indexes of these two columns.