Session Ready
Exercise

Read a fixed-width file

Sometimes you will run into files containing columns of data that are separated by whitespace and all line up on one side (like how R prints data frames to the console). These files can be read by readr's read_table() function.

Inside names.txt are a few famous names with states and (fake) phone numbers. When you import it, you will also set the name of each column by passing a vector to the col_names argument.

After your first import, you'll notice that there is a missing value in the state column that was left as a character string instead of being converted to NA. We can fix that by specifying the na argument to include entries of "NA" or "null".

Instructions
100 XP
  • Import names.txt using read_table(). Pass a vector containing "name", "state", and "phone" as the col_names argument. Call the resulting data frame names.
  • View the head of names.
  • Import the same file using the same function, but this time specify the na argument as a character vector containing "NA" and "null".
  • View the head of names again.