[R] Re gular Expression help

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Sat Nov 8 22:28:54 CET 2008


Wacek Kusnierczyk wrote:
> Gabor Grothendieck wrote:
>   
>> Here are a few more solutions.  x is the input vector
>> of character strings.
>>
>> The first is a slightly shorter version of one of Wacek's.
>> The next three all create an anonymous grouping variable
>> (using sub, substr/gsub and strapply respectively)
>> whose components are "p" and "q" and then tapply
>> is used to separate out the corresponding components
>> of x according to the grouping:
>>
>> sapply(c(p = "^[^pq]*p", q = "^[^pq]*q"), grep, x = x, value = TRUE)
>>
>> tapply(x, sub("^[^pq]*(.).*", "\\1", x), c)
>>
>> tapply(x, substr(gsub("[^pq]", "", x), 1, 1), c)
>>
>> library(gsubfn)
>> tapply(x, strapply(x, "^[^pq]*(.)", simplify = c), c)
>>   
>>     
>
> wow!  cool stuff.  if you're interested in comparing their efficiency,
> source the attached script.
>
>   

using lapply with side-effects code should probably be considered bad
practice, so replace lapply with a for loop.  sorry.

vQ
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: pq.r
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20081108/590a7aaa/attachment.pl>


More information about the R-help mailing list