I am trying to use spread
as part of the tidyr
package. I have one key
column (structure_type
) and four value
columns. (structure_type
contains three factor levels.) All the documentation I've seen assumes that spread
can only be used for one key
column and one value
column. But it seems like this should be possible to do this. Perhaps I should use the reshape
library instead?
This answer came close but I couldn't reproduce it.
library(tidycensus)
library(tidyr)
units_str_puma <- get_acs(geography = 'public use microdata area', table = "B25024",
state = c('OR', 'WA'))
units_str2_puma <- units_str_puma %>%
mutate(structure_type = case_when(variable == "B25024_001" ~ "Total units",
variable %in% c("B25024_002", "B25024_003") ~ "One-unit structure",
TRUE ~ "Other")) %>%
group_by(structure_type, GEOID) %>%
summarize(units = sum(estimate),
units_moe = moe_sum(moe = moe, estimate = estimate),
units_cv = units_moe/1.645/units,
units_cv_flag = case_when(units_cv > 0.4 ~ 1,
TRUE ~ 0)) %>%
spread(key=structure_type, value=c(units, units_moe, units_cv, units_cv_flag), drop=FALSE)