我正在尝试将spread
用作tidyr
软件包的一部分。我有一列key
列(structure_type
)和四列value
列。 (structure_type
包含三个因子级别。)我所见过的所有文档都假设spread
只能用于一个key
列和一个value
列。但是似乎应该可以做到这一点。也许我应该改用reshape
库?
This answer接近了,但我无法复制。
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)