5.2 Looking at data
You are now ready to load the data. We are going to use a function from the readr
package called read_csv()
to load the data file into memory (as a data.frame
). In this case, our data is in a subdirectory called “data”.
library(readr)
cats <- read_csv(file = 'data/herding-cats-small.csv')
## Parsed with column specification:
## cols(
## street = col_character(),
## coat = col_character(),
## sex = col_character(),
## age = col_double(),
## weight = col_double(),
## fixed = col_integer(),
## wander_dist = col_double(),
## roamer = col_character(),
## cat_id = col_integer()
## )
When you import a file, readr
prints out a message that tells you what type of data it thinks is in each column of your file. By default, readr
is usually able to guess what classes your data belong to, and sets them up for you as a data.frame
. If we want to check that our data has been loaded, we can print the variable’s value: cats
.
cats
The dataset we’re using for the current examples is quite small, and you should be able to see all the data in your console. However, if our dataset was larger, we probably wouldn’t want to print the whole thing to look at it. Instead, we can use the head
command to view the first six lines or the View
command to open the dataset in a spreadsheet-like viewer.
View(cats)
We’ve just done two very useful things.
1. We’ve read our data in to R, so now we can work with it in R
2. We’ve created a data frame (with the read_csv
command) the standard way R works with data.