[R] modifying vector elements

Gabor Grothendieck ggrothendieck at gmail.com
Tue Jul 13 10:24:22 CEST 2010


On Tue, Jul 13, 2010 at 3:20 AM, Lorenzo Cattarino
<l.cattarino at uq.edu.au> wrote:
> Hello R-users,
>
>
>
> I have a very large vector (a) containing elements consisting of numbers
> and letters, this is the  i.e.
>
> a
>
> [1] "1.11.2a"     "1.11.2d"     "1.11.2e"     "1.11.2f"     "1.11.2x1"
> "1.11.2x1b"
>
[...]
>
> How can I remove from each record everything that is after the number
> after the second dot? E.g.:
>
> 1.11.2a becomes 1.11.2, 1.12.1x4 becomes 1.12.1, 1.9.1a becomes
> 1.9.1...and so forth.

If they are all of the form shown then the question is equivalent to
removing the first alphabetic character, [[:alpha:]], and everything
thereafter (.*) which is just this.

   sub("[[:alpha:]].*", "", a)



More information about the R-help mailing list