r - how can i extract California county locations from given latitude and longitude information -


i have following dataset california housing data:

head(calif_cluster,15)    medianhousevalue medianincome medianhouseage totalrooms totalbedrooms population 1            190300      4.20510             16    2697.00        490.00       1462 2            150800      2.54810             33    2821.00        652.00       1206 3            252600      6.08290             17    6213.20       1276.05       3288 4            269700      4.03680             52     919.00        213.00        413 5             91200      1.63680             28    3072.00        790.00       1375 6             66200      2.18980             30     744.00        156.00        410 7            148800      2.63640             39     620.95        136.00        348 8            384800      4.46150             20    2270.00        498.00       1070 9            153200      2.75000             22    1931.00        445.00       1009 10            66200      1.60057             36     973.00        219.00        613 11           461500      3.78130             43    3070.00        668.00       1240 12           144600      2.85000             22    5175.00       1213.00       2804 13           143700      5.09410              8    6213.20       1276.05       3288 14           195500      5.30620             16    2918.00        444.00       1697 15           268800      2.42110             22     620.95        136.00        348    households latitude longitude cluster_kmeans gender_dom marital     race edu_level rental 1         515    38.48   -122.47              1          m   other    black jrcollege rented 2         640    38.00   -122.13              1          f   other hispanic doctorate  owned 3        1162    33.88   -117.79              3          m   other    white jrcollege  owned 4         193    37.85   -122.25              1          m  single   others jrcollege  owned 5         705    38.13   -122.26              1          f  single    white doctorate rented 6         165    38.96   -122.21              1          f  single   others jrcollege  owned 7         125    34.01   -118.18              2          m married   others  postgrad  owned 8         521    33.83   -118.38              2          f  single    white  graduate rented 9         407    38.95   -121.04              1          m married   others  postgrad leased 10        187    35.34   -119.01              2          m  single hispanic doctorate  owned 11        646    33.76   -118.12              2          f   other   others  highschl leased 12       1091    37.95   -122.05              3          m   other    white  graduate rented 13       1162    36.87   -119.75              3          m   other   others  postgrad leased 14        444    32.93   -117.13              2          m   other    asian jrcollege  owned 15        125    37.71   -120.98              1          f  single    asian  postgrad leased 

as have latitude & longitude information in datasets, extract corresponding county given geo information using r. possible getting capital city(or largest city) each of extracted counties .these make stratified analysis more insightful;intend clustering/mapping exercise.

take @ ggmap::revgeocode

code

library(ggmap) revgeocode(c(-122.47,38.48)) # longitude latitude # [1] "2233 sulphur springs ave, st helena, ca 94574, usa"  library(dplyr) library(magrittr) df12 %<>% rowwise %>% mutate(address = revgeocode(c(longitude,latitude))) %>% ungroup        # add full address using google api through ggmap df12 %<>% separate(address,c("street_address", "city","county","country"),remove=f,sep=",")  # structure info need 

result

df12 %>% select(longitude,latitude,address,county) # tibble: 15 x 4 # longitude latitude                                            address    county # *     <dbl>    <dbl>                                              <chr>     <chr> #  1   -122.47    38.48 2233 sulphur springs ave, st helena, ca 94574, usa  ca 94574 #  2   -122.13    38.00    3400-3410 brookside dr, martinez, ca 94553, usa  ca 94553 #  3   -117.79    33.88  19721 bluefield plaza, yorba linda, ca 92886, usa  ca 92886 #  4   -122.25    37.85             6365 florio st, oakland, ca 94618, usa  ca 94618 #  5   -122.26    38.13              119 mimosa ct, vallejo, ca 94589, usa  ca 94589 #  6   -122.21    38.96              unnamed road, arbuckle, ca 95912, usa  ca 95912 #  7   -118.18    34.01    4360-4414 noakes st, los angeles, ca 90023, usa  ca 90023 #  8   -118.38    33.83    903 serpentine st, redondo beach, ca 90277, usa  ca 90277 #  9   -121.04    38.95        14666-14690 musso rd, auburn, ca 95603, usa  ca 95603 # 10   -119.01    35.34           800 ming ave, bakersfield, ca 93307, usa  ca 93307 # 11   -118.12    33.76   6211-6295 e marina dr, long beach, ca 90803, usa  ca 90803 # 12   -122.05    37.95              1120 carey dr, concord, ca 94520, usa  ca 94520 # 13   -119.75    36.87        1815-1899 e pryor dr, fresno, ca 93720, usa  ca 93720 # 14   -117.13    32.93      9010-9016 danube ln, san diego, ca 92126, usa  ca 92126 # 15   -120.98    37.71       748-1298 claribel rd, modesto, ca 95356, usa  ca 95356 

data

df1 <- read.table(text = "medianhousevalue medianincome medianhouseage totalrooms totalbedrooms population     1            190300      4.20510             16    2697.00        490.00       1462                       2            150800      2.54810             33    2821.00        652.00       1206                       3            252600      6.08290             17    6213.20       1276.05       3288                       4            269700      4.03680             52     919.00        213.00        413                       5             91200      1.63680             28    3072.00        790.00       1375                       6             66200      2.18980             30     744.00        156.00        410                       7            148800      2.63640             39     620.95        136.00        348                       8            384800      4.46150             20    2270.00        498.00       1070                       9            153200      2.75000             22    1931.00        445.00       1009                       10            66200      1.60057             36     973.00        219.00        613                       11           461500      3.78130             43    3070.00        668.00       1240                       12           144600      2.85000             22    5175.00       1213.00       2804                       13           143700      5.09410              8    6213.20       1276.05       3288                       14           195500      5.30620             16    2918.00        444.00       1697                       15           268800      2.42110             22     620.95        136.00        348",header=t,stringsasfactors=f)   df2 <- read.table(text = "households latitude longitude cluster_kmeans gender_dom marital     race edu_level rental                   1         515    38.48   -122.47              1          m   other    black jrcollege rented                   2         640    38.00   -122.13              1          f   other hispanic doctorate  owned                   3        1162    33.88   -117.79              3          m   other    white jrcollege  owned                   4         193    37.85   -122.25              1          m  single   others jrcollege  owned                   5         705    38.13   -122.26              1          f  single    white doctorate rented                   6         165    38.96   -122.21              1          f  single   others jrcollege  owned                   7         125    34.01   -118.18              2          m married   others  postgrad  owned                   8         521    33.83   -118.38              2          f  single    white  graduate rented                   9         407    38.95   -121.04              1          m married   others  postgrad leased                   10        187    35.34   -119.01              2          m  single hispanic doctorate  owned                   11        646    33.76   -118.12              2          f   other   others  highschl leased                   12       1091    37.95   -122.05              3          m   other    white  graduate rented                   13       1162    36.87   -119.75              3          m   other   others  postgrad leased                   14        444    32.93   -117.13              2          m   other    asian jrcollege  owned                   15        125    37.71   -120.98              1          f  single    asian  postgrad leased",header=t,stringsasfactors=f)  df12 <- cbind(df1,df2) 

i don't think library offers option capital or largest city in county think won't have trouble building lookup table online info.


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -