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 withlevels.
In this exercise, you will work with hotdogs.txt (view), which is a tab-delimited file without column names in the first row.
Diese Übung ist Teil des Kurses
Introduction to Importing Data in R
Anleitung zur Übung
hotdogsis created for you without setting the column types. Inspect its summary using thesummary()function.- Two collector functions are defined for you:
facandint. Have a look at them, do you understand what they're collecting? - In the second
read_tsv()call, edit thecol_typesargument: Pass alist()with the elementsfac,intandint, so the first column is imported as a factor, and the second and third column as integers. - Create a
summary()ofhotdogs_factor. Compare this to the summary ofhotdogs.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
# Import without col_types
hotdogs <- read_tsv("hotdogs.txt", col_names = c("type", "calories", "sodium"))
# Display the summary of hotdogs
___
# The collectors you will need to import the data
fac <- col_factor(levels = c("Beef", "Meat", "Poultry"))
int <- col_integer()
# Edit the col_types argument to import the data correctly: hotdogs_factor
hotdogs_factor <- read_tsv("hotdogs.txt",
col_names = c("type", "calories", "sodium"),
col_types = NULL)
# Display the summary of hotdogs_factor
___