-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcross_tabulation.rmd
32 lines (21 loc) · 1.48 KB
/
cross_tabulation.rmd
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
27
28
29
30
31
32
---
title: "2. Cross-tabulation"
output: md_document
---
# Creating a cross-tablulation
The next step is to add additional variables to the tabulations that we went through in the first tutorial. This tutorial should be much shorter now that the basics are out of the way. We'll continue using the poverty_new variable that we created. The code to load the libraries and data, then recode the variable is below.
```{r, setup}
library(car)
library(dplyr)
# Change the file path to the proper location
# setwd(PATH_TO_TUTORIAL_FOLDER) #uncomment this line to change directory.
# reads in the csv file and stores it. stringsAsFactors=FALSE keeps R from tansforming variables you don't want transformed.
data <- read.csv("Data and Documentation/ss14pco.csv", stringsAsFactors=FALSE)
data <- data%>% # passes the data object we created above to the next function
mutate(poverty_new=recode(POVPIP, "000:124=1; 125:183=2; 183:501=3")) # adds the poverty_new variable to the end of the dataset, it doesn't over write POVPIP.
```
Let's say that we want to know about children and poverty using the same poverty levels we used before. We'd need to create another variable, based on the `AGEP` variable (see the data dictionary page 32) that indicated if a person was under 18 or not. To do this, we'll use the base R command `ifelse` to create a statement that will make those under 18 coded to 1 and those 18 and over coded 2.
```{r, recode}
data <- data%>%
mutate(child=ifelse(AGEP<18, 1, 2))
```