Skip to content

character position

7 messages · Paulo Cardoso, jim holtman, Dimitris Rizopoulos +2 more

#
Hi,

I'm not being able to capture a position of a 'string' in a character
string.

In this example: 'There are 20 species in this grid'

I would like to capture the string (number) after 'are' and before
'species'. Consider they do not change. I wouldn't like to use substr
because stop position may change.

Thanks

Paulo
#
If it always occurs after 'are', this will work:
[1] "20"

        
On Thu, Sep 18, 2008 at 4:51 AM, Paulo Cardoso <pecardoso at netcabo.pt> wrote:

  
    
#
you could use gsub(), e.g.,

strg <- "There are 20 species in this grid"
gsub("[^0-9]", "", strg)


I hope it helps.

Best,
Dimitris
Paulo Cardoso wrote:

  
    
#
Perfect but when one wants to get something different, like

x <- 'INPUT FILE record.dat'
sub(".*FILE (\\w+).*", "\\1", x, perl=TRUE) 

will return 'record' and not 'record.dat'

the [:punct:] is not retained.

Paulo
#
Try:

sub(".*FILE (\\w+)|\\.*", "\\1", x, perl=TRUE)
On Thu, Sep 18, 2008 at 7:39 AM, Paulo Cardoso <pecardoso at netcabo.pt> wrote:
--
Henrique Dallazuanna
Curitiba-Paran?-Brasil
25? 25' 40" S 49? 16' 22" O
#
Thank you all. Works fine.
#
Try this:

library(gsubfn)
strapply("There are 23 species", "[0-9]+", as.numeric)[[1]]

The gsubfn home pae is at:
http://gsubfn.googlecode.com
On Thu, Sep 18, 2008 at 4:51 AM, Paulo Cardoso <pecardoso at netcabo.pt> wrote: