PC Labs for SO5041: Week 1
Table of Contents
Week 1
Installing R and RStudio
Working with R
We begin by downloading a data file (an extract from the US National Longitudinal Survey of Women, 1988).
We first install and then load some some libraries into R, and read the file (installing is needed only once, for new libraries).
Installing needs to be done only once:
install.packages("tidyverse")
install.packages("ggplot2")
If packages are already installed, they are loaded with library() calls.
library(ggplot2)
library(tidyverse)
library(haven)
nlsw88 <- as_factor(read_dta("https://teaching.sociology.ul.ie/so5041/nlsw88.dta"))
names(nlsw88)
The variable nlsw88 is a "data frame" (or "tibble"), where rows are cases/observations and columns are variables. Variables within the dataframe are referred to using $, e.g., nlsw88$occupation.
In the top-right window in RStudio, you will see details of the variables. You can also get the list of variables by entering the command names(nlsw88).
Aside: "pipelines" in R
Instead of
nlsw88 <- as_factor(read_dta("nlsw88.dta"))
we could do
read_dta("nlsw88.dta") |> as_factor() -> nlsw88
Both do the same thing
- read the file as a dta file using
read_dta() - do
as_factor()on the result - store the result in the variable
nlsw88
Some people find the latter format more intuitive.
Tabulating categorical variables
Some of these variables have small numbers of distinct values (e.g., occupation), whereas some are quantities like money or time (e.g., wage). We can summarise the values of categorical variables using frequency tables:
table(nlsw88$occupation)
Often, categorical values have labels. These are best treated as "factors" (when we loaded the data with read_dta() we set all the variables with labels to factors with the as_factor() command; sometimes better done variable by variable).
nlsw88$occupation <- as_factor(nlsw88$occupation) table(nlsw88$occupation)
The function table() gives the default frequency table in R: not very readable. The next command loads a new command, tabxw(), which makes slightly better tables.
source("https://teaching.sociology.ul.ie/so4046/tabxw.R")
(Note that it can be dangerous to run code straight off the web like this: it's generally better to download and inspect it first.)
tabxw(nlsw88, occupation)
Aside: commands vary in style
You will see that some R commands expect simple variables
x <- c(1,2,3) mean(x)
and to access variables within tibbles or data frames we need to use the $:
table(nlsw88$occupation)
And yet other commands understand data frames and know you are talking about variables within a named tibble or data frame:
tabxw(nlsw88, occupation)
Summarising quantitative variables
Variables like occupation or union are categorical, "factors" in R terminology: they have a more-or-less small number of distinct values and can usefully be tabulated. Variables like wage have very large numbers of different values, but can be summarised otherwise:
## Univariate summary of quantitative variable
summary(nlsw88$wage)
mean(nlsw88$wage)
sd(nlsw88$wage)
print(quantile(nlsw88$wage))
Bar-charts
R's ggplot2 library produces high quality graphics. Experiment with these commands:
ggplot(nlsw88, aes(x=occupation)) + geom_bar(color="red", fill="pink")
ggplot(nlsw88, aes(x=occupation)) + geom_bar(color="red", fill="pink") + coord_flip()
ggplot(nlsw88, aes(x=occupation)) + geom_bar(color="red", fill="pink") + coord_flip() + ggtitle("Bar chart: Occupation")
Histograms
Histograms are a good way to summarise quantitative variables. The R default is somewhat ugly but simple:
hist(nlsw88$wage)
ggplot2 is more flexible, if verbose:
ggplot(nlsw88, aes(x=wage)) + geom_histogram() + ggtitle("Histogram of Wage")
ggplot(nlsw88, aes(x=wage)) + geom_histogram(color="white", fill="red") + ggtitle("Histogram of Wage")