Correct! What about column types? We already mentioned that we have character vectors like
country and numeric vectors like
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
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:
- the number of rows and columns in the data frame (rows are called observations and columns are called variables),
- the names of the data frame's columns,
- the column data types (chr means character column, num means numerical column),
- the first several values of each column, followed by a trailing ellipsis.