연도별 차주 인종 및 민족 (II)
이번 연습에서는 iotools와 bigtabulate를 함께 사용해 연도별 차주의 인종과 민족을 집계해 보겠습니다.
이 연습은 강의의 일부입니다
R에서 확장 가능한 데이터 처리
연습 안내
iotools와 bigtabulate가 워크스페이스에 로드되어 있습니다.
- 청크를 행렬로 읽어 들인 뒤, 차주 인종과 연도별로 집계하는 함수
make_table()을 만드세요. - 우리가 만들어 둔 파일 연결에서 데이터를 가져오기 위해
chunk.apply()를 사용하세요. race_year_table을 데이터 프레임으로 변환하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Open a connection to the file and skip the header
fc <- file("mortgage-sample.csv", "rb")
readLines(fc, n = 1)
# Create a function to read chunks
make_table <- function(chunk) {
# Create a matrix
m <- ___(chunk, sep = ",", type = "integer")
colnames(m) <- mort_names
# Create the output table
___(m, c("borrower_race", "year"))
}
# Import data using chunk.apply
race_year_table <- ___(fc, make_table)
# Close connection
close(fc)
# Cast it to a data frame
rydf <- ___(race_year_table)
# Create a new column Race with race/ethnicity
rydf$Race <- race_cat