[R] RE : regular expressions : extracting numbers
GOUACHE David
D.GOUACHE at arvalisinstitutduvegetal.fr
Mon Jul 30 15:39:02 CEST 2007
and I was afraid everyone would be on holidays...
thanks to all 7 of you who answered !!
David Gouache
Arvalis - Institut du Végétal
Station de La Minière
78280 Guyancourt
Tel: 01.30.12.96.22 / Port: 06.86.08.94.32
-----Message d'origine-----
De : Gabor Grothendieck [mailto:ggrothendieck at gmail.com]
Envoyé : lundi 30 juillet 2007 15:05
À : GOUACHE David
Cc : r-help at stat.math.ethz.ch
Objet : Re: [R] regular expressions : extracting numbers
I assume if you want the "" components to be NA then you really intend
the result to be a numeric vector. The following replaces all non-digits
with "" (thereby removing them) and then uses as.numeric to convert the
result to numeric. Just omit the conversion if you want a character
vector result:
s <- c("lema, rb 2%", "rb 2%", "rb 3%", "rb 4%", "rb 3%", "rb 2%,mineuse",
"rb", "rb", "rb 12", "rb", "rj 30%", "rb", "rb", "rb 25%", "rb", "rb",
"rb", "rj, rb")
as.numeric(gsub("[^[:digit:]]+", "", s))
On 7/30/07, GOUACHE David <D.GOUACHE at arvalisinstitutduvegetal.fr> wrote:
> Hello all,
>
> I have a vector of character strings, in which I have letters, numbers, and symbols. What I wish to do is obtain a vector of the same length with just the numbers.
> A quick example -
>
> extract of the original vector :
> "lema, rb 2%" "rb 2%" "rb 3%" "rb 4%" "rb 3%" "rb 2%,mineuse" "rb" "rb" "rb 12" "rb" "rj 30%" "rb" "rb" "rb 25%" "rb" "rb" "rb" "rj, rb"
>
> and the type of thing I wish to end up with :
> "2" "2" "3" "4" "3" "2" "" "" "12" "" "30" "" "" "25" "" "" "" ""
>
> or, instead of "", NA would be acceptable (actually it would almost be better for me)
>
> Anyways, I've been battling with gsub() and things of the sort, but I'm drowning in the regular expressions, despite a few hours of looking at Perl tutorials...
> So if anyone can help me out, it would be greatly appreciated!!
>
> In advance, thanks very much.
>
> David Gouache
> Arvalis - Institut du Végétal
> Station de La Minière
> 78280 Guyancourt
> Tel: 01.30.12.96.22 / Port: 06.86.08.94.32
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
More information about the R-help
mailing list