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
character position
7 messages · Paulo Cardoso, jim holtman, Dimitris Rizopoulos +2 more
If it always occurs after 'are', this will work:
x <- 'There are 20 species in this grid'
y <- sub(".*are (\\d+).*", "\\1", x, perl=TRUE)
y
[1] "20"
On Thu, Sep 18, 2008 at 4:51 AM, Paulo Cardoso <pecardoso at netcabo.pt> wrote:
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
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
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:
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
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
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
-----Original Message----- From: jim holtman [mailto:jholtman at gmail.com] Sent: quinta-feira, 18 de Setembro de 2008 9:58 To: Paulo Cardoso Cc: r Subject: Re: [R] character position If it always occurs after 'are', this will work:
x <- 'There are 20 species in this grid'
y <- sub(".*are (\\d+).*", "\\1", x, perl=TRUE)
y
[1] "20"
On Thu, Sep 18, 2008 at 4:51 AM, Paulo Cardoso <pecardoso at netcabo.pt> wrote:
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
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-
guide.html
and provide commented, minimal, self-contained, reproducible code.
-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? No virus found in this incoming message. Checked by AVG. Version: 8.0.100 / Virus Database: 270.6.21/1676 - Release Date: 17-09- 2008 9:33
Try:
sub(".*FILE (\\w+)|\\.*", "\\1", x, perl=TRUE)
On Thu, Sep 18, 2008 at 7:39 AM, Paulo Cardoso <pecardoso at netcabo.pt> 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
-----Original Message----- From: jim holtman [mailto:jholtman at gmail.com] Sent: quinta-feira, 18 de Setembro de 2008 9:58 To: Paulo Cardoso Cc: r Subject: Re: [R] character position If it always occurs after 'are', this will work:
x <- 'There are 20 species in this grid'
y <- sub(".*are (\\d+).*", "\\1", x, perl=TRUE)
y
[1] "20"
On Thu, Sep 18, 2008 at 4:51 AM, Paulo Cardoso <pecardoso at netcabo.pt> wrote:
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
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-
guide.html
and provide commented, minimal, self-contained, reproducible code.
-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? No virus found in this incoming message. Checked by AVG. Version: 8.0.100 / Virus Database: 270.6.21/1676 - Release Date: 17-09- 2008 9:33
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Thank you all. Works fine.
-----Original Message-----
From: Henrique Dallazuanna [mailto:wwwhsd at gmail.com]
Sent: quinta-feira, 18 de Setembro de 2008 12:14
To: Paulo Cardoso
Cc: jim holtman; r
Subject: Re: [R] character position
Try:
sub(".*FILE (\\w+)|\\.*", "\\1", x, perl=TRUE)
On Thu, Sep 18, 2008 at 7:39 AM, Paulo Cardoso <pecardoso at netcabo.pt>
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
-----Original Message----- From: jim holtman [mailto:jholtman at gmail.com] Sent: quinta-feira, 18 de Setembro de 2008 9:58 To: Paulo Cardoso Cc: r Subject: Re: [R] character position If it always occurs after 'are', this will work:
x <- 'There are 20 species in this grid'
y <- sub(".*are (\\d+).*", "\\1", x, perl=TRUE)
y
[1] "20"
On Thu, Sep 18, 2008 at 4:51 AM, Paulo Cardoso
<pecardoso at netcabo.pt>
wrote:
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
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-
project.org/posting-
guide.html
and provide commented, minimal, self-contained, reproducible
code.
-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? No virus found in this incoming message. Checked by AVG. Version: 8.0.100 / Virus Database: 270.6.21/1676 - Release Date:
17-09-
2008 9:33
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-
guide.html
and provide commented, minimal, self-contained, reproducible code.
-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O No virus found in this incoming message. Checked by AVG. Version: 8.0.100 / Virus Database: 270.6.21/1676 - Release Date: 17-09- 2008 9:33
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:
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
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.