Reading raw data and turning it into a data structure
As mentioned before, part of what makes iotools
fast is that it separates reading data from the hard drive from converting the binary data it into a data.frame
or matrix
. Data in their binary format are copied from the hard drive into memory as raw
objects. These raw
objects are then passed to optimized functions that turn them into data.frame
or matrix
objects.
In this exercise, you'll learn how to separate reading data from the disk (using the readAsRaw()
function), and then convert the raw
binary data into a matrix
or data.frame
(using the mstrsplit()
and dstrsplit()
functions).
This is a part of the course
“Scalable Data Processing in R”
Exercise instructions
- Read
"mortgage-sample.csv"
as a raw vector. - Convert the raw vector contents to a
matrix
of integers. - Convert the raw vector contents to a
data.frame
with 16 integer columns.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Read mortgage-sample.csv as a raw vector
raw_file_content <- ___("mortgage-sample.csv")
# Convert the raw vector contents to a matrix
mort_mat <- ___(___, sep = ",", type = ___, skip = 1)
# Look at the first 6 rows
head(mort_mat)
# Convert the raw file contents to a data.frame
mort_df <- ___(___, sep = ",", col_types = rep("integer", 16), skip = 1)
# Look at the first 6 rows
head(mort_df)