spread()正在生成NA值.(R编程)



我正在使用R库tidycensus数据从census.gov下载数据。然后我使用spread((。每个大地水准面都有许多具有估计值的柱,但它为其余的柱生成NA。

实际数据

应用扩散函数后的数据

请帮我更正数据。

Dput:

structure(list(GEOID = c(13001950100, 13001950100, 13001950100, 
13001950100, 13001950100, 13001950100), NAME = c("Census Tract 9501, Appling County, Georgia", 
"Census Tract 9501, Appling County, Georgia", "Census Tract 9501, Appling County, Georgia", 
"Census Tract 9501, Appling County, Georgia", "Census Tract 9501, Appling County, Georgia", 
"Census Tract 9501, Appling County, Georgia"), variable = c("S2401_C01_001", 
"S2401_C01_002", "S2401_C01_003", "S2401_C01_004", "S2401_C01_005", 
"S2401_C01_006"), estimate = c(1406, 271, 54, 54, 0, 0), moe = c(214, 
87, 43, 43, 13, 13)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

如果您希望每个ID都在一行中:

library(tidyverse)     
df <- df %>%
pivot_wider(names_from = variable, values_from = c("estimate", "moe"))

带有dcast的选项

library(data.table)
dcast(setDT(df), GEOID + NAME ~ variable, value.var = c("estimate", "moe"))

最新更新