R 如何在不删除列的情况下为县名称分配fips代码?
根据@akrun在另一个帖子中的要求,我在这里发布了这个问题: 我正在使用以下函数,它使用R 如何在不删除列的情况下为县名称分配fips代码?,r,dataframe,R,Dataframe,根据@akrun在另一个帖子中的要求,我在这里发布了这个问题: 我正在使用以下函数,它使用usmap包将美国Fips代码分配给各自的县名 pointsDF <- pointsDF %>% group_by(state) %>% summarise(new = list(fips(state = first(state), county = newcounty))) %>% unnest(c(new)) 而不是做总结+取消测试。使用mutate,以便维护其他列 l
usmap
包将美国Fips代码分配给各自的县名
pointsDF <- pointsDF %>%
group_by(state) %>%
summarise(new = list(fips(state = first(state), county = newcounty))) %>%
unnest(c(new))
而不是做
总结
+取消测试
。使用mutate
,以便维护其他列
library(usmap)
library(dplyr)
pointsDF %>%
group_by(state) %>%
mutate(new = fips(state = first(state), county = newcounty))
# lon lat state newcounty new
# <dbl> <dbl> <chr> <chr> <chr>
#1 -95.9 36.1 Oklahoma tulsa 40143
#2 -90.1 32.4 Mississippi madison 28089
#3 -98.3 29.5 Texas bexar 48029
#4 -80.7 35.0 North Carolina union 37179
库(usmap)
图书馆(dplyr)
点SDF%>%
按(州)分组%>%
变异(new=fips(state=first(state),county=newcounty))
#朗拉特州新县新县
#
#1-95.9 36.1俄克拉荷马州塔尔萨40143
#2-90.1 32.4密西西比州麦迪逊28089
#3-98.3 29.5德克萨斯州比克斯48029
#4-80.7 35.0北卡罗来纳州联邦37179
或者保持与最初尝试类似的方法,请执行以下操作:
pointsDF %>%
group_by(state) %>%
summarise(new = list(fips(state = first(state), county = newcounty)),
lon = first(lon), lat = first(lat)) %>%
unnest(c(new))
# state new lon lat
# <chr> <chr> <dbl> <dbl>
# 1 Alabama 01117 -86.8 33.3
# 2 Alabama 01077 -86.8 33.3
# 3 Alabama 01073 -86.8 33.3
# 4 Alabama 01073 -86.8 33.3
# 5 Alabama 01073 -86.8 33.3
# 6 Alabama 01101 -86.8 33.3
# 7 Alabama 01089 -86.8 33.3
# 8 Alabama 01117 -86.8 33.3
# 9 Alabama 01073 -86.8 33.3
#10 Alabama 01117 -86.8 33.3
# … with 431 more rows
pointsDF%>%
按(州)分组%>%
总结(新=列表(FIP(州=第一(州),县=新县)),
lon=第一(lon),lat=第一(lat))%>%
unnest(c(新))
#州新隆拉特
#
#阿拉巴马州01117-86.8 33.3
#2阿拉巴马州01077-86.8 33.3
#3阿拉巴马州01073-86.8 33.3
#4阿拉巴马州01073-86.8 33.3
#5阿拉巴马州01073-86.8 33.3
#6阿拉巴马州01101-86.8 33.3
#7阿拉巴马州01089-86.8 33.3
#8阿拉巴马01117-86.8 33.3
#9阿拉巴马州01073-86.8 33.3
#10阿拉巴马01117-86.8 33.3
#…还有431行
我们可以使用
library(dplyr)
pointsDF %>%
group_by(state) %>%
mutate(new = fips(state = state[1], county = newcounty))
谢谢你的回复。我得到这个错误:错误:
mutate()
inputnew
有问题。x输入<代码>新代码>不能回收到26号。ℹ 输入new
是fips(state=first(state),country=newcounty)
。ℹ 输入new
的大小必须为26或1,而不是25。ℹ 错误发生在组1:state=“Alabama”中。您使用的数据是否与您的帖子中的数据相同?你对这些数据有相同的错误吗?是的,但是我发布的数据只是前四行。我的数据集大约有450行,所以我没有全部发布。我应该编辑文章以包含整个数据集吗?是还是至少在收到多少行错误后检查?该错误表明state=“Alabama”
存在一些问题。只检查那些行。您是否有导致该组出错的异常数据?你有NA
值吗?我看了看,那里没有任何NA
值。数据看起来不错,更让我担心的是,Alabama
是按字母顺序排列的第一组
library(dplyr)
pointsDF %>%
group_by(state) %>%
mutate(new = fips(state = state[1], county = newcounty))