Hello everyone, I have a character vector of 24 hour time values in the format hm without the delimiting ":". How can I insert the ":" immediately to the left of the second digit from the right? mytimes<-scan(what="") 1457 1457 1310 1158 137 1855 Thanks! Dan
Gerrit Eichner
2012-Jan-26 15:00 UTC
[R] Inserting a character into a character string XXXX
Hello, Dan, you could probably use a combination of nchar(), substr() (or substring()) and paste(). Take a look at their online help pages. Hth -- Gerrit> Hello everyone, > > I have a character vector of 24 hour time values in the format hm > without the delimiting ":". How can I insert the ":" immediately to > the left of the second digit from the right? > > mytimes<-scan(what="") > 1457 > 1457 > 1310 > 1158 > 137 > 1855 > > > Thanks! > > Dan > > ______________________________________________ > 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.
William Dunlap
2012-Jan-26 15:41 UTC
[R] Inserting a character into a character string XXXX
> sub("([[:digit:]]{2,2})$", ":\\1", mytimes)[1] "14:57" "14:57" "13:10" "11:58" "1:37" "18:55" That will convert "05" to ":05" and will do nothing to "5". Pad with 0's before calling sub if that is required. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Dan Abner > Sent: Thursday, January 26, 2012 6:50 AM > To: r-help at r-project.org > Subject: [R] Inserting a character into a character string XXXX > > Hello everyone, > > I have a character vector of 24 hour time values in the format hm > without the delimiting ":". How can I insert the ":" immediately to > the left of the second digit from the right? > > mytimes<-scan(what="") > 1457 > 1457 > 1310 > 1158 > 137 > 1855 > > > Thanks! > > Dan > > ______________________________________________ > 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.