用一个键和多个值列传播[tidyr]

时间:2018-12-06 18:21:43

标签: r reshape tidyr spread

我正在尝试将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)

Data structure

0 个答案:

没有答案