Get startedGet started for free

Specifying column classes

The colClasses argument of fread() allows you to explicitly specify the class of the columns. You can specify the class of the columns as a character vector (as is done in base R functions such as read.csv()):

colClasses = c("integer", rep("factor", 3), "character")

However, this format requires you to specify the classes of all columns. This can be problematic if the file contains several columns and you only want to explicitly specify classes for some columns and leave the rest as default. You can use a more convenient list format for this purpose:

colClasses = list(factor = 2:4)

Here, the columns two through four are imported as factors and the rest are imported using fread()'s defaults.

This exercise is part of the course

Data Manipulation with data.table in R

View Course

Hands-on interactive exercise

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

# Import using read.csv with defaults
base_r_defaults <- ___("sample.csv")
___(base_r_defaults)
Edit and Run Code