열 클래스
colClasses 인수를 사용하면 읽어들이는 파일의 각 열에 대해 데이터 유형을 지정할 수 있어요. 이는 가져오기 과정의 효율을 높이고, 각 열이 올바른 데이터 유형으로 읽히도록 보장합니다.
다음과 같이 colClasses 인수를 설정하면 됩니다:
read.delim("my_file.txt",
colClasses = c("character",
"numeric",
"logical"))
colClasses 벡터에서 어떤 열을 "NULL"로 지정하면, 해당 열은 건너뛰고 데이터 프레임에 로드되지 않습니다.
이 연습은 강의의 일부입니다
R에서 데이터 가져오기 소개
연습 안내
hotdogs데이터 프레임이 로드되어 있습니다. 먼저hotdogs의 구조를 표시하세요.- 두 번째
read.delim()호출의colClasses인수에서 1, 2, 3번째 열을 각각'character','NULL','numeric'로 설정하세요. hotdogs2의 구조를 표시하고 차이점을 확인하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# 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
___