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")

 

This entire site is Copyright © 1997-2024 by Courtney Brown. All Rights Reserved.
DISCLAIMER
URL: https://courtneybrown.com