0

I am trying to follow the tutorial outlined here but having trouble

But I am running into a problem at this step:

my_crime <- data.frame(year=my_crime$Year, community=my_crime$Community.Area, 
         type=my_crime$Primary.Type, arrest=my_crime$Arrest, 
         latitude=my_crime$Latitude, longitude=my_crime$Longitude)

My equivalent step is:

geocode <- data.frame(latitude=geocode$lat, longitude=geocode$long)

I get the following error:

Error in geocode$lat : $ operator is invalid for atomic vectors

I made the geocode dataset by sending a list of addresses to the street2coordinates website and getting back a list of long/lats (as outlined here) It seems that something is wrong with the dataset I created coming out of that. Here is the part where I make geocode:

data2 <- paste0("[",paste(paste0("\"",fm$V2,"\""),collapse=","),"]")
data2
url  <- "http://www.datasciencetoolkit.org/street2coordinates"
response <- POST(url,body=data2)
json     <- fromJSON(content(response,type="text"))

geocode  <- do.call(rbind,lapply(json,
                                 function(x) c(address=paste(x$street_address, x$locality, x$region), long=x$longitude,lat=x$latitude)))
geocode

Thank you for any and all help!

Results of str(geocode) after the first do.call (I altered the addresses):

chr [1:2, 1:3] "123 Main St Anytown MA" "669 Main St Anytown MA" "-65.5"     "-33.4" "22.1" ...
  - attr(*, "dimnames")=List of 2

  ..$ : chr [1:2] " 123 Main St Anytown MA" " 669 Main St Anytown MA" 

  ..$ : chr [1:3] "address" "long" "lat" 
Community
  • 1
  • 1
garson
  • 1,505
  • 3
  • 22
  • 56
  • Please post the result of `str(geocode)` after the `do.call`. –  Jun 18 '15 at 01:47
  • I added to the end of the post. Thank you – garson Jun 18 '15 at 01:49
  • "geocode" doesn't look to be a data.frame, rather more a matrix (what gives `class(geocode)`?). In that case, you cannot access columns with `$`. Also, all your values have been coerced into characters. You should change the `do.call` line to directly create a data.frame –  Jun 18 '15 at 02:01
  • yes class(geocode) is 'matrix.' Can you explain what you mean by "change the do.call line to directly create a data frame"? – garson Jun 18 '15 at 02:03

2 Answers2

1

Or you can use the RDSTK package and do the same thing:

library(RDSTK)

data <- c("1208 Buckingham Drive, Crystal Lake, IL 60014",
          "9820 State Street East, Paducah, KY 42001",
          "685 Park Place, Saint Petersburg, FL 33702",
          "5316 4th Avenue, Charlotte, NC 28205",
          "2994 Somerset Drive, Baldwinsville, NY 13027",
          "5457 5th Street South, Tallahassee, FL 32303")

geocode  <- do.call(rbind, lapply(data, street2coordinates))

geocode

##                                    full.address country_code3 latitude
## 1 1208 Buckingham Drive, Crystal Lake, IL 60014           USA 42.21893
## 2     9820 State Street East, Paducah, KY 42001           USA 36.50045
## 3    685 Park Place, Saint Petersburg, FL 33702           USA 27.96470
## 4          5316 4th Avenue, Charlotte, NC 28205           USA 35.22241
## 5  2994 Somerset Drive, Baldwinsville, NY 13027           USA 42.94575
## 6  5457 5th Street South, Tallahassee, FL 32303           USA 30.45489
##    country_name longitude    street_address region confidence
## 1 United States -88.33914 474 Buckingham Dr     IL      0.805
## 2 United States -88.32971      498 State St     KY      0.551
## 3 United States -82.79733       685 Park St     FL      0.721
## 4 United States -80.80540     1698 Firth Ct     NC      0.512
## 5 United States -76.56455   98 Somerset Ave     NY      0.537
## 6 United States -84.29354     699 W 5th Ave     FL      0.610
##   street_number     locality   street_name fips_county country_code
## 1           474 Crystal Lake Buckingham Dr       17111           US
## 2           498        Hazel      State St       21035           US
## 3           685   Clearwater       Park St       12103           US
## 4          1698    Charlotte      Firth Ct       37119           US
## 5            98       Auburn  Somerset Ave       36011           US
## 6           699  Tallahassee     W 5th Ave       12073           US
hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
0

Currently, your do.call creates a matrix (using rbind and c), coercing all the numeric into characters.

The following should turn your list "json" into the data.frame "geocode" will the information you need, i.e. "address", "long" and "lat".

foo <- function(x) data.frame(address=paste(x$street_address, x$locality, 
                              x$region), long=x$longitude,lat=x$latitude)
geocode  <- do.call(rbind, sapply(json, foo))