Courtney Brown, Ph.D.
H O M E
A Brief Biography
Curriculum Vitae
Career Guidance Videos
Political Music Videos
Political Music Articles
R Tutorial Videos
Data Sets and
Computer Programs
Scholarly
Speculative Nonfiction
The Farsight Institute
Book Reviews
Videos
Publicity Photos
Speaking Requests
Farsight Prime
African Television
Music Videos

C O N T A C T
Follow on Facebook Courtney Brown
Follow on FB
Follow on Twitter Courtney Brown
Follow on Twitter
Courtney Brown on Instagram

 

Creating Crosstabulation Tables in R

Below is computer code written in the R programming language that creates crosstabulation tables between two variables in R. Just copy and paste it into R and watch it rip. Then try changing the format of the tables from SAS to SPSS. The data set for this R program can be found HERE.

# First, load a library that will build nice crosstabulation tables
library(gmodels)
# Now we get our data.
mydata <- read.table("panel80.txt")
names(mydata) # This shows us all the variable names.
mynewdata <- mydata[ which(mydata$VOTE <= 2), ] # This gets rid of observations where votes went to minor candidates.
# Now we create an income category variable based on the mean for use in a table.
mynewdata$incomecategories <- ifelse(mynewdata$INC < mean(mynewdata$INC, na.rm = TRUE), 1, 2) # One way to recode variables.
CrossTable(mynewdata$SEX, mynewdata$VOTE, chisq=TRUE, expected = TRUE, format="SAS")
CrossTable(mynewdata$incomecategories, mynewdata$VOTE, chisq=TRUE, expected = TRUE, format="SAS")