[R] Inserting a character into a character string XXXX

2012-01-26 Thread Dan Abner
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@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.


Re: [R] Inserting a character into a character string XXXX

2012-01-26 Thread Gerrit Eichner

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@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.


__
R-help@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.


Re: [R] Inserting a character into a character string XXXX

2012-01-26 Thread William Dunlap
   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-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
 Behalf Of Dan Abner
 Sent: Thursday, January 26, 2012 6:50 AM
 To: r-help@r-project.org
 Subject: [R] Inserting a character into a character string 
 
 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@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.

__
R-help@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.


Re: [R] Inserting a character into a character string XXXX

2012-01-26 Thread Dan Abner
Hi Bill,

Thanks very much for your response.

Can you suggest an approach for the pre-padding? Here is a more
respresentative sample of the values:

mytimes-scan(what=)
1334
2310
39
2300
1556
3
404
37
1320
4
211
2320


Thanks!

Dan



On Thu, Jan 26, 2012 at 10:41 AM, William Dunlap wdun...@tibco.com wrote:
   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-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
 Behalf Of Dan Abner
 Sent: Thursday, January 26, 2012 6:50 AM
 To: r-help@r-project.org
 Subject: [R] Inserting a character into a character string 

 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@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.

__
R-help@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.


Re: [R] Inserting a character into a character string XXXX

2012-01-26 Thread William Dunlap
One way to pad with initial zeros is to convert your
strings to integers and format the integers:
   sprintf(%04d, as.integer(mytimes))
   [1] 1334 2310 0039 2300 1556 0003 0404
   [8] 0037 1320 0004 0211 2320
It has the added benefit that the call to as.integer
will warn you if any supposed integers don't look like
integers.  If you use this approach you don't need the
call to sub():
   tmp - as.integer(mytimes)
   sprintf(%02d:%02d, tmp%/%100, tmp%%100)
   [1] 13:34 23:10 00:39 23:00 15:56 00:03
   [7] 04:04 00:37 13:20 00:04 02:11 23:20
You could also check that tmp%/%100 (the hour) and tmp%%100
(the minute) are in their expected ranges before calling
sprintf.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

 -Original Message-
 From: Dan Abner [mailto:dan.abne...@gmail.com]
 Sent: Thursday, January 26, 2012 8:55 AM
 To: William Dunlap
 Cc: r-help@r-project.org
 Subject: Re: [R] Inserting a character into a character string 
 
 Hi Bill,
 
 Thanks very much for your response.
 
 Can you suggest an approach for the pre-padding? Here is a more
 respresentative sample of the values:
 
 mytimes-scan(what=)
 1334
 2310
 39
 2300
 1556
 3
 404
 37
 1320
 4
 211
 2320
 
 
 Thanks!
 
 Dan
 
 
 
 On Thu, Jan 26, 2012 at 10:41 AM, William Dunlap wdun...@tibco.com wrote:
    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-boun...@r-project.org [mailto:r-help-boun...@r-project.org] 
  On Behalf Of Dan Abner
  Sent: Thursday, January 26, 2012 6:50 AM
  To: r-help@r-project.org
  Subject: [R] Inserting a character into a character string 
 
  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@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.

__
R-help@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.


Re: [R] Inserting a character into a character string XXXX

2012-01-26 Thread Dan Abner
Cheers Bill!!



On Thu, Jan 26, 2012 at 12:03 PM, William Dunlap wdun...@tibco.com wrote:
 One way to pad with initial zeros is to convert your
 strings to integers and format the integers:
   sprintf(%04d, as.integer(mytimes))
   [1] 1334 2310 0039 2300 1556 0003 0404
   [8] 0037 1320 0004 0211 2320
 It has the added benefit that the call to as.integer
 will warn you if any supposed integers don't look like
 integers.  If you use this approach you don't need the
 call to sub():
   tmp - as.integer(mytimes)
   sprintf(%02d:%02d, tmp%/%100, tmp%%100)
   [1] 13:34 23:10 00:39 23:00 15:56 00:03
   [7] 04:04 00:37 13:20 00:04 02:11 23:20
 You could also check that tmp%/%100 (the hour) and tmp%%100
 (the minute) are in their expected ranges before calling
 sprintf.

 Bill Dunlap
 Spotfire, TIBCO Software
 wdunlap tibco.com

 -Original Message-
 From: Dan Abner [mailto:dan.abne...@gmail.com]
 Sent: Thursday, January 26, 2012 8:55 AM
 To: William Dunlap
 Cc: r-help@r-project.org
 Subject: Re: [R] Inserting a character into a character string 

 Hi Bill,

 Thanks very much for your response.

 Can you suggest an approach for the pre-padding? Here is a more
 respresentative sample of the values:

 mytimes-scan(what=)
 1334
 2310
 39
 2300
 1556
 3
 404
 37
 1320
 4
 211
 2320


 Thanks!

 Dan



 On Thu, Jan 26, 2012 at 10:41 AM, William Dunlap wdun...@tibco.com wrote:
    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-boun...@r-project.org [mailto:r-help-boun...@r-project.org] 
  On Behalf Of Dan Abner
  Sent: Thursday, January 26, 2012 6:50 AM
  To: r-help@r-project.org
  Subject: [R] Inserting a character into a character string 
 
  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@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.

__
R-help@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.