CommencerCommencer gratuitement

Column classes

The colClasses argument allows you to specify the data types for each column of the file you are reading. This can improve the efficiency of the import process and ensure that the columns are read in with the correct data types.

You can do this by setting the colClasses argument:

read.delim("my_file.txt", 
           colClasses = c("character",
                          "numeric",
                          "logical"))

If a column is set to "NULL" in the colClasses vector, this column will be skipped and will not be loaded into the data frame.

Cet exercice fait partie du cours

Introduction to Importing Data in R

Afficher le cours

Instructions

  • The hotdogs data frame has been loaded. Go ahead and display the structure of hotdogs.
  • In the colClasses argument of the second read.delim() call, set the first, second, and third columns to 'character', 'NULL' and 'numeric'.
  • Display the structure of hotdogs2 and look for the difference.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# Previous call to import hotdogs.txt
hotdogs <- read.delim("hotdogs.txt", header = FALSE, col.names = c("type", "calories", "sodium"))

# Display structure of hotdogs
___

# Edit the colClasses argument to import the data correctly: hotdogs2
hotdogs2 <- read.delim("hotdogs.txt", header = FALSE, 
                       col.names = c("type", "calories", "sodium"),
                       colClasses = c(___, ___, ___))


# Display structure of hotdogs2
___
Modifier et exécuter le code