[R] string to numeric question

Henrik Bengtsson hb at stat.berkeley.edu
Wed Sep 24 23:59:24 CEST 2008


See ?sprintf, e.g. x2Str <- sprintf("%02d", as.integer(x)) where 'x2'
is numeric.  Use x <- as.numeric(xStr) to convert string 'xStr' to
numeric and x <- x + 1 to change the value.

/HB

On Wed, Sep 24, 2008 at 2:51 PM,  <markleeds at verizon.net> wrote:
> this seems l ike it shouldn't be that hard  but i give up.
>
> if i have a string say, temp<-"01", I want to increase it by 1 so that it
> becomes "02". but the following code obviously won't work when the input
> string is say "10" because then it gives "011" when I just want "11".
> uuugh.
>
> does someone know an easy way ( i guess hard is fine too. beggars can't be
> choosy )  around that ?  the code below probably explains more
> clearly  what i mean. thanks.
>
> temp1 <- "01"
> paste("0",as.numeric(temp1)+1,sep="")
> temp10 <- "10"
> paste("0",as.numeric(temp10)+1,sep="")
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list