1. Learn
  2. /
  3. Courses
  4. /
  5. Reading Data into R with readr

Exercise

Reading a .csv file

Welcome to this mini-course on the readr package! Over the next two chapters, you’ll be exposed to many of the great features this package has to offer.

Let’s get started right away with some basic importing features. Your first task will be to master the use of the read_csv() function. There are many arguments available, but the only required argument is file, a path to a CSV file on your computer (or the web).

If you've imported data in the past, you've probably used the base R function read.csv(). One big advantage that read_csv() has over read.csv() is that it doesn't convert strings into factors by default. Instead, read_csv() recognizes 8 different data types (integer, logical, etc.) and leaves anything else as characters. That means you don't have to set stringsAsFactors to FALSE every time you import a CSV file with character strings!

You’ll practice your importing skills on a dataset containing weights of chickens and their feed type.

Instructions

100 XP
  • Load the readr package.
  • Use the read_csv() function to import chickwts.csv. Call the resulting data frame cwts.
  • View the head() of cwts.