MulaiMulai sekarang secara gratis

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.

Latihan ini adalah bagian dari kursus

Introduction to Importing Data in R

Lihat Kursus

Petunjuk latihan

  • 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.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

# 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
___
Edit dan Jalankan Kode