[R] Extract last 3 characters from numeric vector

Kang Min ngokangmin at gmail.com
Mon Dec 5 04:02:44 CET 2011


Thanks Sarah, but I discovered that numbers like 3.0 only have 1
character, so I had a range of character lengths from 1 to 4 (e.g.
17.0 has 2 characters, 3.4 has 3 and 12.4 has 4).

Uwe's method worked well. Thanks again.

Kang Min

On Dec 4, 11:42 pm, Uwe Ligges <lig... at statistik.tu-dortmund.de>
wrote:
> On 04.12.2011 14:38, Kang Min wrote:
>
> > Hi all,
>
> > I have a numeric vector with 1 decimal place, and I'd like to extract
> > the last 3 characters, including the decimal point. The vector ranges
> > from 0 to 20.
>
> > x<- round(runif(100)*20, digits=1)
>
> formatC(round(x%%10, 1), format="f", digits=1)
>
> Uwe Ligges
>
> > Some of numbers have 3 characters, and some have 4. I've read up on
> > the substr() function but that extracts characters based on exact
> > positions. How can I extract just the last 3 characters no matter the
> > length of the number? e.g. from 16.7 I want 6.7, from 3.5 I want 3.5
> > as it is.
>
> > Thanks,
> > Kang Min
>
> > ______________________________________________
> > R-h... at r-project.org mailing list
> >https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list