Skip to content
Back to formatted view

Raw Message

Message-ID: <AANLkTimK94dYEPDb7=vdTr=KdfPDhbKkHi0WNfJ8ghot@mail.gmail.com>
Date: 2011-02-04T19:22:28Z
From: Gabor Grothendieck
Subject: lapply, strsplit, and list elements
In-Reply-To: <B37C0A15B8FB3C468B5BC7EBC7DA14CC63430F92AA@LP-EXMBVS10.CO.IHC.COM>

On Fri, Feb 4, 2011 at 1:27 PM, Greg Snow <Greg.Snow at imail.org> wrote:
> Try this:
>
>> x <- c("349/077,349/074,349/100,349/117",
> + ? ? ? ? ?"340/384.2,340/513,367/139,455/128,D13/168",
> + ? ? ? ? ?"600/437,128/903,128/904")
>>
>> library(gsubfn)
>> out <- strapply(x, '([0-9]+)(?=/)')
>> out
> [[1]]
> [1] "349" "349" "349" "349"
>
> [[2]]
> [1] "340" "340" "367" "455" "13"
>
> [[3]]
> [1] "600" "128" "128"
>
>
> The strapply looks for the pattern then returns every time it finds the pattern. ?The pattern in this case is 1 or more digits that are followed by a /, but the slash is not included in the matched portion (a positive look ahead).
>
> If you need more than digits you can modify the pattern to whatever matches before the /.

Also this similar approach with a slight simplification of the regular
expression:

   strapply(x, '([0-9]+)/')

or to convert the numbers to numeric at the same time:

   strapply(x, '([0-9]+)/', as.numeric)


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com