Election Examples

Please see Dr. Ward’s following video below for more exercise with tapply function in R. The 1980 election data is used in this video, and the tapply function is used to find the amount of money donated in each state.

This is the code to read the data:

library(data.table)
myDF <- fread("/anvil/projects/tdm/data/election/itcont1980.txt", quote="")
names(myDF) <- c("CMTE_ID", "AMNDT_IND", "RPT_TP", "TRANSACTION_PGI", "IMAGE_NUM", "TRANSACTION_TP", "ENTITY_TP", "NAME", "CITY", "STATE", "ZIP_CODE", "EMPLOYER", "OCCUPATION", "TRANSACTION_DT", "TRANSACTION_AMT", "OTHER_ID", "TRAN_ID", "FILE_NUM", "MEMO_CD", "MEMO_TEXT", "SUB_ID")

Let’s take the money for the donations (in the TRANSACTION_AMT column), group the data according to the state (in the STATE column), and sum up the donation amounts:

tapply(myDF$TRANSACTION_AMT, myDF$STATE, sum)