Get Started

col_types

You can also specify which types the columns in your imported data frame should have. You can do this with col_types. If set to NULL, the default, functions from the readr package will try to find the correct types themselves. You can manually set the types with a string, where each character denotes the class of the column: character, double, integer and logical. _ skips the column as a whole.

potatoes.txt (view), a flat file with tab-delimited records and without column names, is again available in your workspace.

This is a part of the course

“Introduction to Importing Data in R”

View Course

Exercise instructions

  • In the second read_tsv() call, edit the col_types argument to import all columns as characters (c). Store the resulting data frame in potatoes_char.
  • Print out the structure of potatoes_char and verify whether all column types are chr, short for character.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Column names
properties <- c("area", "temp", "size", "storage", "method",
                "texture", "flavor", "moistness")

# Import all data, but force all columns to be character: potatoes_char
potatoes_char <- read_tsv("potatoes.txt", col_types = "iiiiiddd", col_names = properties)

# Print out structure of potatoes_char
___

This exercise is part of the course

Introduction to Importing Data in R

BeginnerSkill Level
4.2+
26 reviews

In this course, you will learn to read CSV, XLS, and text files in R using tools like readxl and data.table.

In addition to base R, there are dedicated packages to easily and efficiently import flat file data. We'll talk about two such packages: readr and data.table.

Exercise 1: readr: read_csv & read_tsvExercise 2: read_csvExercise 3: read_tsvExercise 4: readr: read_delimExercise 5: read_delimExercise 6: skip and n_maxExercise 7: col_types
Exercise 8: col_types with collectorsExercise 9: data.table: freadExercise 10: freadExercise 11: fread: more advanced useExercise 12: Dedicated classes

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free