TDM 10100: Project 4 Question 3 examples
Example 1
In base R, we can read data into a base R data frame using read.csv, for example, as follows:
myDF = read.csv("/anvil/projects/tdm/data/icecream/combined/reviews.csv")
Now we can check that we read it in properly:
head(myDF)
and we can even summarize a column to be sure:
table(myDF$brand)
It is faster to use fread rather than read.csv but we need to first load the data.table library. The fread function actually reads data into a data.table.
library(data.table)
myDF = fread("/anvil/projects/tdm/data/icecream/combined/reviews.csv")
Now we can check that everything works the same as before:
table(myDF$brand)
Example 2
After we have already imported the data.table library, we do not need to re-import it. We can just read in more data frames (actually a data.table), e.g., like this:
myDF = fread("/anvil/projects/tdm/data/flights/subset/airports.csv")
Do not worry too much about the warning! If you want to discuss the warning with Dr Ward, please feel welcome to ask him during seminar. One of the rows of data has some double-quotes in it, and fread is just letting you know.
Now we can check that we read it in properly:
head(myDF)
and we can even check which states have the most airports:
tail(sort(table(myDF$state)))