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. I will appreciate any help. Peter Maclean Department of Economics UDSM [[alternative HTML version deleted]]
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
Hi, Try: x$Log <-? gsub("^(\\d{2})(\\d{2})\\.(\\d{3})","\\1:\\2:\\3E",sprintf("%.3f",x$Log)) x$lat <- gsub("^(\\d{1})(\\d{2})\\.(\\d{3})","\\1:\\2:\\3S",sprintf("%.3f",x$lat)) ?x #???????? Log?????? lat #1 30:25:264E 9:01:331S #2 30:39:237E 8:10:811S #3 31:37:760E 8:06:040S #4 31:41:190E 8:06:557S #5 31:41:229E 8:06:622S #6 31:38:891E 8:06:281S A.K. On Wednesday, October 23, 2013 5:07 AM, Peter Maclean <pmaclean2011 at yahoo.com> 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. I will appreciate any help. Peter Maclean Department of Economics UDSM ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.