0

由于我的另一个问题已经结束,这里是所需的数据。我想要做的是让 R 计算最后一列“计数”到列城市,这样我就可以映射数据。因此我需要某种代码来匹配这个。因为我想显示有多少参与者(计数)处于夏威夷(HI)等州

zip     city         state  latitude    longitude   count
96860   Pearl Harbor    HI  24.859832   -168.021815 36
96863   Kaneohe Bay     HI  21.439867   -157.74772  39
99501   Anchorage       AK  61.216799   -149.87828  12
99502   Anchorage       AK  61.153693   -149.95932  17
99506   Elmendorf AFB   AK  61.224384   -149.77461  2

我试过的是

match<- c(match(datazip$state, datazip$number))>$

但我真的很无奈试图找到一个解决方案,因为我什至不知道如何简单地描述这个。之后我的计划是用数据制作等值线图,相信我现在我已经看到了几乎所有试图提供建议的页面。所以非常感谢您的帮助。谢谢

4

2 回答 2

2
# I read your sample data to a data frame
> df
    zip          city state latitude longitude count
1 96860  Pearl_Harbor    HI 24.85983 -168.0218    36
2 96863   Kaneohe_Bay    HI 21.43987 -157.7477    39
3 99501     Anchorage    AK 61.21680 -149.8783    12
4 99502     Anchorage    AK 61.15369 -149.9593    17
5 99506 Elmendorf_AFB    AK 61.22438 -149.7746     2

# If you want to sum the number of counts by state
library(plyr)
> ddply(df, .(state), transform, count2 = sum(count))
    zip          city state latitude longitude count count2
1 99501     Anchorage    AK 61.21680 -149.8783    12     31
2 99502     Anchorage    AK 61.15369 -149.9593    17     31
3 99506 Elmendorf_AFB    AK 61.22438 -149.7746     2     31
4 96860  Pearl_Harbor    HI 24.85983 -168.0218    36     75
5 96863   Kaneohe_Bay    HI 21.43987 -157.7477    39     75
于 2012-10-15T11:50:49.040 回答
0

也许aggregate对您来说是一个不错且简单的解决方案:

df
    zip          city state latitude longitude count
1 96860  Pearl Harbor    HI 24.85983 -168.0218    36
2 96863   Kaneohe Bay    HI 21.43987 -157.7477    39
3 99501     Anchorage    AK 61.21680 -149.8783    12
4 99502     Anchorage    AK 61.15369 -149.9593    17
5 99506 Elmendorf AFB    AK 61.22438 -149.7746     2

aggregate(df$count,by=list(df$state),sum)
  Group.1  x
1      AK 31
2      HI 75

aggregate(df$count,by=list(df$city),sum)
        Group.1  x
1     Anchorage 29
2 Elmendorf AFB  2
3   Kaneohe Bay 39
4  Pearl Harbor 36
于 2012-10-15T12:15:47.083 回答