Skip to content

Commit

Permalink
fixed tibble
Browse files Browse the repository at this point in the history
  • Loading branch information
dinglucy committed Mar 27, 2024
1 parent 98dfdb4 commit 8f78262
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions R/parity.R
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ optimal_transfer <- function(map, plan, county_splits = FALSE, no_transfer = dat

# Generate a list of all of the district population deviations
gen_move_list <- function(redist_map, plan, n_dists){
map <- as_tibble(redist_map)
map <- tibble::as_tibble(redist_map)
map$plan <- plan
pop_ideal <- floor(sum(redist_map$pop)/n_dists)
move_list <- map %>%
Expand Down Expand Up @@ -257,7 +257,7 @@ gen_adj_matrix <- function(redist_map, plan, county_splits = FALSE, no_transfer

if (county_splits){
# Create list of counties in each district
counties <- as_tibble(redist_map) %>%
counties <- tibble::as_tibble(redist_map) %>%
group_by(plan) %>%
summarize(county = list(unique(county)))

Expand Down
22 changes: 11 additions & 11 deletions R/walnuts.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ walnuts_blk <- function(map_prec, map_blk, plan, tolerance = 5) {

# If no more changes to be made return block-level plan
if (nrow(transfer) == 0 | sum(transfer$pop) %in% c(-1, 0, 1)){
return(as_tibble(map_blk) %>% select(GEOID, BLOCKID, plan))
return(tibble::as_tibble(map_blk) %>% select(GEOID, BLOCKID, plan))
}

# Order transfer by largest population
Expand Down Expand Up @@ -102,15 +102,15 @@ walnuts_blk <- function(map_prec, map_blk, plan, tolerance = 5) {
# Get new plan
map_blk$new_plan <- NULL
map_blk <- map_blk %>%
left_join(as_tibble(new_map)
left_join(tibble::as_tibble(new_map)
%>% select(BLOCKID, new_plan), by = "BLOCKID") %>%
mutate(plan = ifelse(is.na(new_plan), plan, new_plan))

# Calculate new transfer
new_transfer <- optimal_transfer(map_blk, map_blk$plan)

if (nrow(new_transfer) == 0){
return(as_tibble(map_blk) %>% select(GEOID, BLOCKID, plan))
return(tibble::as_tibble(map_blk) %>% select(GEOID, BLOCKID, plan))
}

new_transfer <- new_transfer %>%
Expand Down Expand Up @@ -151,7 +151,7 @@ walnuts_blk <- function(map_prec, map_blk, plan, tolerance = 5) {
}

# Returns precinct GEOIDs, block GEOIDs, and plan
return(as_tibble(map_blk) %>% select(GEOID, BLOCKID, plan))
return(tibble::as_tibble(map_blk) %>% select(GEOID, BLOCKID, plan))
}

# Walnuts functions ---------------------------------------------------------
Expand All @@ -163,7 +163,7 @@ walnuts_prec_2dists <- function(map, plan, transfer, dist_1, dist_2, shattered =
original_map <- map

# Convert redist_map to df
map <- as_tibble(map)
map <- tibble::as_tibble(map)

# Join plan to redist_map
map$plan <- plan
Expand Down Expand Up @@ -303,7 +303,7 @@ walnuts_prec <- function(map, plan){
# Get new plan
map$new_plan <- NULL
map <- map %>%
left_join(as_tibble(new_map) %>%
left_join(tibble::as_tibble(new_map) %>%
select(GEOID, new_plan), by = "GEOID") %>%
mutate(plan = ifelse(is.na(new_plan), plan, new_plan))

Expand Down Expand Up @@ -341,7 +341,7 @@ walnuts_blk_2dists <- function(map, plan, transfer, dist_1, dist_2, gpp, shatter
original_map <- map

# Create dataframe
map <- as_tibble(map)
map <- tibble::as_tibble(map)

# Join plan to redist_map
map$plan <- plan
Expand Down Expand Up @@ -487,11 +487,11 @@ find_gpp_2dists <- function(map_prec, map_blk, plan, dist_1, dist_2){

scale <- function(x){(x-min(x))/(max(x)-min(x))}

gpp <- as_tibble(map_blk) %>%
gpp <- tibble::as_tibble(map_blk) %>%
filter(to_swap == TRUE) %>%
group_by(GEOID) %>%
summarize(count = n(), pop_on_boundary = sum(pop)) %>%
left_join(as_tibble(map_prec) %>% select(GEOID, pop), by = "GEOID") %>%
left_join(tibble::as_tibble(map_prec) %>% select(GEOID, pop), by = "GEOID") %>%
mutate_at(c('count', 'pop_on_boundary', 'pop'), scale) %>%
mutate(score = count*pop_on_boundary*pop) %>%
arrange(desc(score)) %>%
Expand All @@ -510,11 +510,11 @@ order_gpp_2dists <- function(map_prec, map_blk, plan, dist_1, dist_2){

scale <- function(x){(x-min(x))/(max(x)-min(x))}

gpp <- as_tibble(map_blk) %>%
gpp <- tibble::as_tibble(map_blk) %>%
filter(to_swap == TRUE) %>%
group_by(GEOID) %>%
summarize(count = n(), pop_on_boundary = sum(pop)) %>%
left_join(as_tibble(map_prec) %>% select(GEOID, pop), by = "GEOID") %>%
left_join(tibble::as_tibble(map_prec) %>% select(GEOID, pop), by = "GEOID") %>%
mutate_at(c('count', 'pop_on_boundary', 'pop'), scale) %>%
mutate(score = count*pop_on_boundary*pop) %>%
arrange(desc(score)) %>%
Expand Down

0 comments on commit 8f78262

Please sign in to comment.