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
Instructions
- The
hotdogs
data frame has been loaded. Go ahead and display the structure ofhotdogs
. - In the
colClasses
argument of the secondread.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
___