尝试使用map_data函数提取州和县

时间:2018-05-14 01:16:40

标签: r

我正在尝试运行以下内容:

library(ggplot2)
library(RColorBrewer)
state_df <- map_data('state')
county_df <- map_data('county')

transform_mapdata <- function(x){
names(x)[5:6] <- c('state','county')
for(u in c('state','county'){
x[,u] <- sapply(x[,u],simpleCap)
}
return(x)
}
state_df <- transform_mapdata(state_df)
county_df <- transform_mapdata(county_df)

我一直收到这条消息:

Error in x[, u] : incorrect number of dimensions
>     }
Error: unexpected '}' in "    }"

数据似乎没问题,所以。我想这个问题与转型有关。

> head(state_df)
       long      lat group order  region subregion
1 -87.46201 30.38968     1     1 alabama      <NA>
2 -87.48493 30.37249     1     2 alabama      <NA>
3 -87.52503 30.37249     1     3 alabama      <NA>
4 -87.53076 30.33239     1     4 alabama      <NA>
5 -87.57087 30.32665     1     5 alabama      <NA>
6 -87.58806 30.32665     1     6 alabama      <NA>
> head(county_df)
       long      lat group order  region subregion
1 -86.50517 32.34920     1     1 alabama   autauga
2 -86.53382 32.35493     1     2 alabama   autauga
3 -86.54527 32.36639     1     3 alabama   autauga
4 -86.55673 32.37785     1     4 alabama   autauga
5 -86.57966 32.38357     1     5 alabama   autauga
6 -86.59111 32.37785     1     6 alabama   autauga

1 个答案:

答案 0 :(得分:2)

第一个问题似乎是缺少的括号:

"for(u in c('state','county'){"

应该是:

for(u in c('state','county')){

虽然修复后出现此错误: Error in sapply(x[, u], simpleCap) : object 'simpleCap' not found