[R] Geocode Conversion

Jim Lemon jim at bitwrit.com.au
Wed Oct 23 11:27:16 CEST 2013


On 10/23/2013 08:05 PM, Peter Maclean wrote:
> I have this kind od  data
> x<- read.table(text="
>   Log     lat
> 3025.264 901.331
> 3039.237 810.811
> 3137.760 806.040
> 3141.190 806.557
> 3141.229 806.622
> 3138.891 806.281",
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  header=TRUE,
> stringsAsFactors=FALSE)
> I what to insert “:” after every first
> two numbers in Lon variable and after
> every first number in lat variable.
> The variables should end with “E”  for
> Lon and “S” for the Lon variable.  The
> results should look like:
> Lon                      Lat
> 30:25:264EÂ Â Â Â Â  Â  9:01:331S
> 30:39:237EÂ Â Â Â Â  Â  8:10:811S
> 31:37:760EÂ Â Â Â Â Â  Â 8:06:040S
> 31:41:190EÂ Â Â Â Â  Â  8:06:557S
> 31:41:229EÂ Â Â Â Â  Â  8:06:622S
> 31:38:891EÂ Â Â  Â Â Â  8:06:281S
> I am using gmt package for
> geocodes conversion.

Hi Peter,
Kind of messy, but:

insert.colons.lon<-function(x) {
  newx<-paste(x[1],x[2],":",x[3],x[4],":",x[6],sep="")
  if(length(x) > 6) newx<-paste(newx,x[7],sep="")
  if(length(x) > 7) newx<-paste(newx,x[8],sep="")
  newx<-paste(newx,"E",sep="")
  return(newx)
}

insert.colons.lat<-function(x) {
  newx<-paste(x[1],":",x[2],x[3],":",x[5],sep="")
  if(length(x) > 5) newx<-paste(newx,x[6],sep="")
  if(length(x) > 6) newx<-paste(newx,x[7],sep="")
  newx<-paste(newx,"S",sep="")
  return(newx)
}

x$lon<-unlist(sapply(strsplit(as.character(x$lon),insert.colons.lon)))
x$lat<-unlist(sapply(strsplit(as.character(x$lat),insert.colons.lat)))

Jim



More information about the R-help mailing list