Session Ready
Exercise

col_types with collectors

Another way of setting the types of the imported columns is using collectors. Collector functions can be passed in a list() to the col_types argument of read_ functions to tell them how to interpret values in a column.

For a complete list of collector functions, you can take a look at the collector documentation. For this exercise you will need two collector functions:

  • col_integer(): the column should be interpreted as an integer.
  • col_factor(levels, ordered = FALSE): the column should be interpreted as a factor with levels.

In this exercise, you will work with hotdogs.txt, which is a tab-delimited file without column names in the first row.

Instructions
100 XP
  • hotdogs is created for you without setting the column types. Inspect its summary using the summary() function.
  • Two collector functions are defined for you: fac and int. Have a look at them, do you understand what they're collecting?
  • In the second read_tsv() call, edit the col_types argument: Pass a list() with the elements fac, int and int, so the first column is importead as a factor, and the second and third column as integers.
  • Create a summary() of hotdogs_factor. Compare this to the summary of hotdogs.