bapuvaitla / ca-political-power

Modeling of political power distribution in California
0 stars 0 forks source link

prep.R still not working #1

Closed msmerlak closed 5 years ago

msmerlak commented 5 years ago

A small bug, but haven't been able to fix it myself. I think a comma is missing when extracting columns names from the templates.


>   unzip("./2016/acs_2016_template.zip", exdir = "./2016/acs_5yr/")
>   temp <- list.files(path = "./2016/acs_5yr/templates/", pattern = "Seq.*.xls",
+                        full.names = T)
>   acs_16_template <- lapply(temp, read_xls)
>   acs_16_template <- lapply(acs_16_template, function(x)                                       
+       x <- setDT(x))
>   file.remove("./2016/acs_2016_template.zip")
[1] TRUE
>   acs_id_cols <- acs_16_5y[[1]][1:6]
>   acs_id_template <- acs_16_template[[1]][1:6]
>   acs_16_5y <- lapply(acs_16_5y, function(x)
+     x <- x %>% rename_at(vars(1:6), ~ names(acs_id_template)))
 Error: `nm` must be `NULL` or a character vector the same length as `x`
Call `rlang::last_error()` to see a backtrace >         # Add GEOIDs
>   download.file(
+     "https://www2.census.gov/programs-surveys/acs/summary_file/2016/documentation/geography/5yr_year_geo/ca.xlsx", "./2016/geoid_ca.xlsx"
+     )
trying URL 'https://www2.census.gov/programs-surveys/acs/summary_file/2016/documentation/geography/5yr_year_geo/ca.xlsx'
downloaded 1.4 MB

>   geoid_ca <- read_xlsx("./2016/geoid_ca.xlsx")
>   acs_16_5y <- lapply(acs_16_5y, function(x)                                                   
+     x <- x %>% mutate(LOGRECNO = sprintf("%07d", LOGRECNO)))
 Error in sprintf("%07d", LOGRECNO) : object 'LOGRECNO' not found >   acs_16_5y <- lapply(acs_16_5y, function(x)
+     x <- merge(geoid_ca, x, by = "LOGRECNO"))
 Error in fix.by(by.y, y) : 'by' must specify a uniquely valid column >         # Merging data and templates (if integrated data table needed)
>   acs_16_5y_int <- lapply(acs_16_5y, function(x)
+     x <- x[, (1:6) := NULL])
>   acs_16_5y_int <- bind_cols(acs_16_5y_int)
>   acs_16_5y_int <- bind_cols(acs_id_cols, acs_16_5y_int)
Error: Argument 2 must be length 6, not 31269
>   acs_16_template_int <- lapply(acs_16_template, function(x)
+     x <- x[, (1:6) := NULL])
>   acs_16_template_int <- bind_cols(acs_16_template_int)
>   acs_16_template_int <- bind_cols(acs_id_template, acs_16_template_int)
Error: Argument 2 must be length 6, not 1
>   names(acs_16_5y_int) <- names(acs_16_template_int)
>   
>     # Convert ACS block group data to SR precincts----
>       # Aggregate block conversion file to block groups
>   sr_blk_g16 <- sr_blk_g16[c("SRPREC", "GEOID","BLKREG")]
>   sr_blk_g16 <- sr_blk_g16 %>% group_by(SRPREC, GEOID) %>%
+     summarise_at("BLKREG", sum, na.rm = T)
>   sr_blk_g16 <- sr_blk_g16 %>% group_by(GEOID) %>%
+     mutate(BLKGRP_TOT = sum(BLKREG))
>   sr_blk_g16 <- sr_blk_g16 %>% mutate(PCTBLK = BLKREG/BLKGRP_TOT)
>     # Convert ACS data to SR
>   acs_16_prec <- lapply(acs_16_5y, function(x) 
+     x <- merge(sr_blk_g16, x, by = "GEOID"))
 Error in fix.by(by.y, y) : 'by' must specify a uniquely valid column >   acs_16_prec <- lapply(acs_16_prec, function(x)
+     x <- x %>% select(-c("GEOID", "BLKREG", "BLKGRP_TOT", "LOGRECNO", "STATE", 
+                          "Name", "FILEID", "FILETYPE", "STUSAB", "CHARITER",
+                          "SEQUENCE")))
Error in lapply(acs_16_prec, function(x) x <- x %>% select(-c("GEOID",  : 
  object 'acs_16_prec' not found
>   acs_16_prec <- lapply(acs_16_prec, function(x)
+     x <- x %>% mutate_at(vars(-SRPREC, -PCTBLK), funs(.*PCTBLK)))
Error in lapply(acs_16_prec, function(x) x <- x %>% mutate_at(vars(-SRPREC,  : 
  object 'acs_16_prec' not found
>   acs_16_prec <- lapply(acs_16_prec, function(x)
+     x <- x %>% group_by(SRPREC) %>% summarize_at(vars(-PCTBLK), sum))
Error in lapply(acs_16_prec, function(x) x <- x %>% group_by(SRPREC) %>%  : 
  object 'acs_16_prec' not found
>   
>   # 2018----
> ```
bapuvaitla commented 5 years ago

Debugged prep.R file pushed to repo