i trying use package ggmap
caculate distance target address list of addresse. data in csv looks below:
order id address 1652049 435 e 70th st,10021 1652123 1365 york ave,10021 1652053 530 e 72nd st,10021
so try distance input address address example: 400 hudson st,10013, , have following code in r:
library(ggmap) mydata<-read.csv("address.csv") mydata$address<-as.character(mydata$address) mydata$distance<-na a<-c("289 hudson st,10013") mydata$distance<-mapdist(mydata$address,a)$miles
however code gives me error message below:
error in `$<-.data.frame`(`*tmp*`, "distance", value = c(8.2403854, 8.2403854, : replacement has 53 rows, data has 31
make sure column names don't have spaces; instead of name "order id", use "order_id". have each address own separate string:
library(ggmap) mydata$address<-as.character(mydata$address) mydata$distance<-na a<-c("289 hudson st,10013") mydata$distance<-mapdist(mydata$address,a)$miles
output:
order_id address distance 1 1652049 435 e 70th st,10021 8.240385 2 1652123 1365 york ave,10021 8.475275 3 1652053 530 e 72nd st,10021 8.618197
sample data:
mydata <- data.frame(order_id=c(1652049,1652123,1652053), address=c('435 e 70th st,10021','1365 york ave,10021', '530 e 72nd st,10021'))
edit:
note in above data, each address own string within vector c(). can see case use of single quotation marks around each address. reason prevent mixing data in case of using csv files, have columns comma-separated. before reading csv file r has commas in columns, address column above, make sure each value/cell in column saved own string i've done (i.e. surrounded single quotes).
Comments
Post a Comment