[R] Gregexpr - extract results with lapply

Lapointe, Pierre Pierre.Lapointe at nbf.ca
Tue Nov 7 13:58:59 CET 2006


Gregexpr - extract results with lapply

Hello,

I need to extract sequences of three upper case letters in a string. In
other words, in this string:

		str <-c("ABC", "this WOUld be gOOD")

The result I'm looking for is ABC WOU OOD.

With gregexpr, I can get the position and length of the sequences

		gregexpr('[A-Z]{3}',str,perl=TRUE)

			[[1]]
			[1] 1
			attr(,"match.length")
			[1] 3

			[[2]]
			[1]  6 16
			attr(,"match.length")
			[1] 3 3

and with substr, I can extract  the data.  (I use 1 and 3 as start and stop
in this example, since I haven't figured out how to use the gregexpr
results.

		substr(str,1,3)

My problem is linking it all together.  I know the answer is probably with
lapply

		lapply(str,substr,1,3)

but I don't know how to specify the right start and stop positions from the
gregexpr results.


**************************************************
AVIS DE NON-RESPONSABILITE: Ce document transmis par courrie...{{dropped}}



More information about the R-help mailing list