Wednesday, June 4, 2014

Boomers vs. Millenials ...


Political post for this code is here.



Hadley Wickham has said that one of the problems with R is that it isn't very "programmerly".  In working through R loops in complex functions, I have discovered this. Here is what I want to do:

for (i in 1:5)  {VDB_M <- as.data.frame(table(VDB_N%i%$BirthDate)) ... }

where %i% is the set of variables I want to loop through. If there is a method for doing that, I can not find it yet.  Clearly a Python or Powershell wrapper might be the answer.   But it is a big language and maybe applying functions in R for the data analyst needs more research on my part.



setwd("C:/Politics")
VDB_20110614 <- read.delim("Ferris061411_20110614_151456_Cdr.txt")
VDB_20121204 <- read.delim("ferrisvoterlist_20121204.txt")
VDB_20131203 <- read.delim("RyanFerrisVoterList_20131203_080700.txt")
VDB_05.20.2014 <- read.delim("05.20.2014.txt")
VDB <- VDB_05.20.2014 #<--Work with the database you like...

accyear <- function() {
VDB_N1 <- subset(VDB, BallotCounted_1 !="NA")
VDB_N2 <-  subset(VDB, BallotCounted_2 !="NA")
VDB_N3 <-  subset(VDB, BallotCounted_3 !="NA")
VDB_N4 <-  subset(VDB, BallotCounted_4 !="NA")
VDB_N5 <-  subset(VDB, BallotCounted_5 !="NA")
VDB_M <- as.data.frame(table(VDB_N1$BirthDate)) # <-- Change N1 to N2 to N3 ....
VDB_M$Year <- substr((VDB_M$Var1),7,10)
VDB_M$Year <- as.numeric(VDB_M$Year)
 ddf <- data.frame(cbind(Year=0,Freq=0))
 blist <- sort(unique(VDB_M$Year))
 for(j in blist) {
 dd <- (data.frame(cbind(Year=j,(subset(VDB_M, Year == j, select= Freq)))))
 dd <- (data.frame(cbind(Year=(unique(dd$Year)),Freq=(cumsum(dd$Freq)[nrow(dd)]))))
 ddf <- rbind(ddf,dd)
}
 ddf <- droplevels(data.frame(ddf[-1,],row.names=NULL))
dev.new()
systime <- as.numeric(Sys.time())
     jpeg(filename = systime,
          width = 1024, height = 768, units = "px", pointsize = 12,
          quality = 100, bg = "white", res = NA, family = "", restoreConsole = TRUE,
          type = c("windows"))
barplot(ddf$Freq,names.arg=ddf$Year,xlab="Voter Birth Year",ylab="Registration Count",ylim=c(0,2750))
graphics.off()
}
accyear()

No comments:

Post a Comment