[R] regular expressions : extracting numbers
Jacques VESLOT
Jacques.Veslot at avignon.inra.fr
Mon Jul 30 14:57:55 CEST 2007
> gsub(" ", "", gsub("%", "", gsub("[a-z]", "", c("tr3","jh40%qs dqd"))))
[1] "3" "40"
Jacques VESLOT
INRA - Biostatistique & Processus Spatiaux
Site Agroparc 84914 Avignon Cedex 9, France
Tel: +33 (0) 4 32 72 21 58
Fax: +33 (0) 4 32 72 21 84
GOUACHE David a écrit :
> 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