I'm reading a csv file which contains coordinates of destination and origin and calculating the distance between origin and destination.
I want to check if any of the cordinates are blank or zero then skip that row and append "0" in another column else the calculated distance, here is the code
library(XML)
library(RCurl)
latlon2ft <- function(origin,destination){
xml.url <- paste0('http://ift.tt/19WUt3A',origin,'&destinations=',destination,'&mode=driving&sensor=false')
xmlfile <- xmlParse(getURL(xml.url))
dist <- xmlValue(xmlChildren(xpathApply(xmlfile,"//distance")[[1]])$value)
distance <- as.numeric(sub(" km","",dist))
ft <- distance/1000
return(ft)
}
ahg = list()
for(i in 1:dim(df2)[1]){
print(i)
if(df2[i,4] || df2[i,5] || df2[i,2] || df2[i,3] == 0) {
df2$Dist = "0"} else {
ahg[[i]] = latlon2ft(origin=paste(as.character(df2[i,4:5]),collapse = ","),destination= paste(as.character(df2[i,2:3]),collapse = ","))
df2$Dist = ahg[[i]]
}
}
but for this code the O/P of newly calculated column Dist always gives "0". What m I missing in the code.any help would be appreciated.
Thanks. Here is sample dataframe
dput(head(df2,5))
structure(list(PROJECT_ID = c(29003L, 16717L, 6807L, 18993L,
16099L), PXVAL = c(0.0000, 18.5504, 18.56816491, 18.7580765,
18.5921), PYVAL = c(0.0000, 73.9412, 73.7800169, 73.6109224,
73.7738), LAT = c(18.529349, 18.529349, 18.529349, 18.529349,
18.529349), LNG = c(73.852879, 73.852879, 73.852879, 73.852879,
73.852879), Dist = c("0", "0", "0", "0", "0")), .Names = c("PROJECT_ID",
"PXVAL", "PYVAL", "LAT", "LNG", "Dist"), spec = structure(list(
cols = structure(list(PROJECT_ID = structure(list(), class = c("collector_integer",
"collector")), PXVAL = structure(list(), class = c("collector_double",
"collector")), PYVAL = structure(list(), class = c("collector_double",
"collector")), LAT = structure(list(), class = c("collector_double",
"collector")), LNG = structure(list(), class = c("collector_double",
"collector"))), .Names = c("PROJECT_ID", "PXVAL", "PYVAL",
"LAT", "LNG")), default = structure(list(), class = c("collector_guess",
"collector"))), .Names = c("cols", "default"), class = "col_spec"), row.names = c(NA,
5L), class = c("tbl_df", "tbl", "data.frame"))
Expected O/P
ID PXVAL PYVAL LAT LNG Dist
29003 0.0000 0 18.529349 73.852879 0
16717 18.5504 73.9412 18.529349 73.852879 12.3
6807 18.56816491 73.7800169 18.529349 73.852879 20.5
18993 18.7580765 73.6109224 18.529349 73.852879 8.10
16099 18.5921 73.7738 18.529349 73.852879 45.2
Aucun commentaire:
Enregistrer un commentaire