R data analysis of the 2016 US election demographics
To get a flavor of the resources available to R developers, we can look at the 2016 election data. In this case, I am drawing from Wikipedia (https://en.wikipedia.org/wiki/United_States_presidential_election,_2016), specifically the table named 2016 presidential vote by demographic subgroup. We have the following coding below.
Define a helper function so we can print out values easily. The new printf
function takes any arguments passed (...)
and passes them along to sprintf
:
printf <- function(...)print(sprintf(...))
I have stored the separate demographic statistics into different TSV (tab-separated value) files, which can be read in using the following coding. For each table, we use the read.csv
function and specify the field separator as a tab instead of the default comma. We then use the head
function to display information about the data frame that was loaded:
age <- read.csv("Documents/B05238_05_age.tsv", sep="\t")head(age)education...