【问题标题】:spread() is producing NA values.(R programming)spread() 正在产生 NA 值。(R 编程)
【发布时间】:2020-04-02 16:12:04
【问题描述】:

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

actual data

data after applying spread function

请帮我更正数据。

输入:

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"))

【问题讨论】:

  • 您可以将数据发布为代码而不是图像吗?

标签: r tidyverse spread


【解决方案1】:

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

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

【讨论】:

  • 太棒了。如果这解决了您的问题,请随时单击左侧的复选标记以表明它已被接受。
【解决方案2】:

dcast 的选项

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

【讨论】:

    猜你喜欢
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 2016-10-17
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    相关资源
    最近更新 更多