我正在使用R通过“ tidycensus”提取人口普查数据,但是它将同一地理位置的不同变量拉到行中,而不是使用单行地理位置和多个变量列。
我尝试了各种转置,聚集和扩展功能,但是无法将扩展值折叠到单行中。我的代码如下:
Median_Inc<-get_acs(geography="County Subdivision",table=B06011,state="MA",county="Middlesex","Essex","Suffolk","Plymouth","Norfolk","Worcester")
会生成一个表:
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
我希望得到这些结果,但是我想做的是生成一个表,该表的所有值都包含一行,并且其中的列是变量名,例如:
GEOID NAME B06011_001 B06011_002 B06011_003 B06011_004 B06011_005
2500901260 Amesbury Town city, Essex County, Massachusetts 37891 37402 47925 NA 27303
答案 0 :(得分:0)
我没有更改get_acs
函数,但是只需很少的操作,就可以拥有想要的东西。
名为tab的原始数据:
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