折叠命名行并将变量移动到 R 中的列

Collapsing Named Rows and Moving Variables to Columns in R

我正在使用 R 通过 'tidycensus' 提取人口普查数据,但它会将同一地理区域的不同变量提取到行中,而不是使用单行地理区域和多个变量列。

我尝试了各种转置、收集和散布函数,但无法将散布值折叠成一行。我的代码如下所示:

Median_Inc<-get_acs(geography="County Subdivision",table=B06011,state="MA",county="Middlesex","Essex","Suffolk","Plymouth","Norfolk","Worcester")

生成 table:

2500901260  Amesbury Town city, Essex County, Massachusetts B06011_001  37891
2500901260  Amesbury Town city, Essex County, Massachusetts B06011_002  37402
2500901260  Amesbury Town city, Essex County, Massachusetts B06011_003  47925
2500901260  Amesbury Town city, Essex County, Massachusetts B06011_004  NA
2500901260  Amesbury Town city, Essex County, Massachusetts B06011_005  27303

我期待这些结果,但我想要做的是生成一个 table,其中包含所有值的一行,其中列是变量名称,例如:

GEOID   NAME    B06011_001  B06011_002  B06011_003  B06011_004  B06011_005
2500901260  Amesbury Town city, Essex County, Massachusetts 37891   37402   47925   NA  27303

我没有更改 get_acs 功能,但只需少量操作,您就可以得到想要的东西。

原始数据命名标签:

         Num                 City        County          State        Code   value
1 2500901260   Amesbury Town city  Essex County  Massachusetts  B06011_001   37891
2 2500901260   Amesbury Town city  Essex County  Massachusetts  B06011_002   37402
3 2500901260   Amesbury Town city  Essex County  Massachusetts  B06011_003   47925
4 2500901260   Amesbury Town city  Essex County  Massachusetts  B06011_004      NA
5 2500901260   Amesbury Town city  Essex County  Massachusetts  B06011_005   27303

要有列名:

colnames(tab) <- c("Num", "City", "County", "State", "Code", "value")

操作后:

library(reshape2)
data_wide <- dcast(tab, Num + City + County + State ~ Code, value.var="value")

     Num                 City        County          State  B06011_001  B06011_002  B06011_003  B06011_004  B06011_005
1 2500901260   Amesbury Town city  Essex County  Massachusetts       37891       37402       47925          NA       27303