Section A.5 Exploring data with R
Subsection A.5.1 Playing around with data
Now that we know the basic component, let’s play around with using R as we will throughout the book, for some data analysis. We will get some data by installing a package which has data in it as well as functions, and then go on to produce some basic summaries. This should give some practice!
We are going to look at some data that are part of the fivethirtyeight package. This package contains datasets and code behind the stories in this particular online magazine (
fivethirtyeight.com). This package is not part of the base installation of R, so you will need to install it first.
Remember, first we have to load the package if we want to use it:
library("fivethirtyeight")
data(package="fivethirtyeight") #Show all data frames available in named package
Notice that this package has some datasets that relate to stories covered in this newspaper that had a criminological angle. Let’s look for example at the
hate_crimes dataset. How do you do that? First, we have to load the data frame into our global environment. To do so use the following code:
data("hate_crimes")
This function will search among all the loaded packages and locate the
hate_crimes dataset. Notice that it now appears in the global environment, although it also says "promise" next to it. To see the data in full, you need to do something to it first. So let’s do that.
Every object in R can have attributes. These are names, dimensions (for matrices and arrays: number of rows and columns) and dimension names, class of object (numeric, character, etc.), length (for a vector this will be the number of elements in the vector), and other user-defined ones. You can access the attributes of an object using the
attributes() function. Let’s query R for the attributes of this data frame.
attributes(hate_crimes)
## $row.names ## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ## [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 ## [51] 51 ## ## $class ## [1] "tbl_df" "tbl" "data.frame" ## ## $names ## [1] "state" "state_abbrev" ## [3] "median_house_inc" "share_unemp_seas" ## [5] "share_pop_metro" "share_pop_hs" ## [7] "share_non_citizen" "share_white_poverty" ## [9] "gini_index" "share_non_white" ## [11] "share_vote_trump" "hate_crimes_per_100k_splc" ## [13] "avg_hatecrimes_per_100k_fbi"
This prints out the row names (not very exciting here...) the class (see above when we used
class() function) and the names, which are the column headers — or the names of the variables within this dataset. You can see there are things like state, and share who voted for Trump in the 2016 election.
Now use the
View() function to glance at your data frame. What you get there is a spreadsheet with 12 variables and 51 observations. Each variable in this case is providing you with information (demographics, voting patterns, and hate crime) about each of the US states.
Ok, let’s now have a quick look at the data. There are so many different ways of producing summary stats for data stored in R that is impossible to cover them all! We will just introduce a few functions that you may find useful for summarising data. Before we do any of that, it is important you get a sense for what is available in this dataset. Go to the help tab and in the search box input the name of the data frame, this will take you to the documentation for this data frame. Here you can see a list of the available variables.
Let’s start with the mean. This function takes as an argument the numeric variable for which you want to obtain the mean. If you want to obtain the mean of the variable that gives us the proportion of people that voted for Donald Trump, you can use the following expression:
mean(hate_crimes$share_vote_trump)
## [1] 0.49
Another function you may want to use with numeric variables is
summary():
summary(hate_crimes$share_vote_trump)
## Min. 1st Qu. Median Mean 3rd Qu. Max. ## 0.0400 0.4150 0.4900 0.4900 0.5750 0.7000
This gives you the five number summary (minimum, first quartile, median, third quartile, and maximum, plus the mean and the count of missing values if there are any).
You don’t have to specify a variable; you can ask for these summaries from the whole data frame:
summary(hate_crimes)
## state median_household_income share_unemployed_seasonal ## Length:51 Min. :35521 Min. :0.02800 ## Class :character 1st Qu.:48657 1st Qu.:0.04200 ## Mode :character Median :54916 Median :0.05100 ## Mean :55224 Mean :0.04957 ## 3rd Qu.:60719 3rd Qu.:0.05750 ## Max. :76165 Max. :0.07300 ## ## share_population_in_metro_areas share_population_with_high_school_degree ## Min. :0.3100 Min. :0.7990 ## 1st Qu.:0.6300 1st Qu.:0.8405 ## Median :0.7900 Median :0.8740 ## Mean :0.7502 Mean :0.8691 ## 3rd Qu.:0.8950 3rd Qu.:0.8980 ## Max. :1.0000 Max. :0.9180 ## ## share_non_citizen share_white_poverty gini_index share_non_white ## Min. :0.01000 Min. :0.04000 Min. :0.4190 Min. :0.0600 ## 1st Qu.:0.03000 1st Qu.:0.07500 1st Qu.:0.4400 1st Qu.:0.1950 ## Median :0.04500 Median :0.09000 Median :0.4540 Median :0.2800 ## Mean :0.05458 Mean :0.09176 Mean :0.4538 Mean :0.3157 ## 3rd Qu.:0.08000 3rd Qu.:0.10000 3rd Qu.:0.4665 3rd Qu.:0.4200 ## Max. :0.13000 Max. :0.17000 Max. :0.5320 Max. :0.8100 ## NA's :3 ## share_vote_trump hate_crimes_per_100k_splc avg_hatecrimes_per_100k_fbi ## Min. :0.040 Min. :0.06745 Min. : 0.2669 ## 1st Qu.:0.415 1st Qu.:0.14271 1st Qu.: 1.2931 ## Median :0.490 Median :0.22620 Median : 1.9871 ## Mean :0.490 Mean :0.30409 Mean : 2.3676 ## 3rd Qu.:0.575 3rd Qu.:0.35694 3rd Qu.: 3.1843 ## Max. :0.700 Max. :1.52230 Max. :10.9535 ## NA's :4 NA's :1
There are multiple ways of getting results in R. Particularly for basic- and intermediate-level statistical analysis many core functions and packages can give you the answer that you are looking for. For example, there are a variety of packages that allow you to look at summary statistics using functions defined within those packages. You will need to install these packages before you can use them.
We are only going to introduce one of them here:
skimr. You will need to install it before anything else.
library(skimr)
Once you have loaded the skimr package you can use it. Its main function is skim. Like summary for data frames,
skim() presents results for all the columns, and the statistics will depend on the class of the variable.
skim(hate_crimes)
Hopefully in your statistical modules you have taken previously, you have learned some things about how to graphically display variables. So you may have some memory about the amount of work involved with this. Hopefully R will offer some respite. Of course, there are many different ways of producing graphics in R. In this course we rely on a package called ggplot2, which is part of the tidyverse set of packages mentioned earlier.
library(ggplot2)
Then we will use one of its functions to create a scatterplot.
ggplot(hate_crimes, aes(x=share_vote_trump, y=avg_hatecrimes_per_100k_fbi)) +
geom_point(shape=1) +
geom_smooth(method=lm)
Graphing is very powerful in R, and much of the spatial visualisation we will produce throughout the book will build on this. If you are not already familiar with this, we recommend a read of the data visualisation chapter of [216].
