Readr
Summary

Instruction

So, with a bit of digging, we were able to fix that problem. Of course, it would have been nice to know in advance that some parts of the city use letters in their address numbers! But now that we know that, we can use another function to manually set column types.

To do this, we add the col_types argument to the read_csv() function.
Note: col_types works with the other read functions as well.

Suppose we know that the ages in the students data should be of an integer data type. When reading the file into memory, we'd write:

students <- read_csv("winters_students.csv",
  col_types = cols(age = col_integer())

Pay attention to the construction of this option. The cols() function has as its argument the name of the column we would like to change. The columns we don't want to change don't have to be mentioned. Next, we have col_integer(). This specifies the type of data in this column. There are other options that work with cols; we could also write col_character() or col_double() for those data types.

Exercise

Read the information in data/companies.csv to memory. Use read_csv() and change the data type of the street_number column to character. Use col_types with the correct cols' arguments. Assign the results to the companies variable.

Stuck? Here's a hint!

Type:

companies <- read_csv("data/companies.csv", 
  col_types = cols(
    street_number = col_character()
  ))