Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction to data frames
Data frame structure
7. Determining the column types of a data frame
Accessing columns in a data frame
Accessing rows and columns combined in a data frame
Data frame analysis
Summary

Instruction

Correct! What about column types? We already mentioned that we have character vectors like city and country and numeric vectors like longitude, latitude, and population in the cities data frame. But how can we be sure about what data types they store?

In R, you can use the str() function for this purpose. As with the other functions we've examined so far, str() takes an object as its argument (in this case a data frame). It then displays the names of the data frame's columns and their corresponding data types, as well as some additional details. For example, a call to

str(cities)
will retrieve the following:

'data.frame': 205 obs. of 5 variables:
$ country : chr "France" "Germany" "Germany" "Germany" ...
$ city : chr "Paris" "Berlin" "Frankfurt" "Stuttgart" ...
$ latitude : num 48.9 52.5 50.1 48.8 53.6 ...
$ longitude : num 2.33 13.4 8.68 9.2 10 ...
$ population: num 4957588 32500.4 1787332 1775644 1748058 ...

As you can tell, the call to str() revealed lots of useful information:

  1. the number of rows and columns in the data frame (rows are called observations and columns are called variables),
  2. the names of the data frame's columns,
  3. the column data types (chr means character column, num means numerical column),
  4. the first several values of each column, followed by a trailing ellipsis.

Exercise

Display the column data types of the data frame countries. Use the str() function.