Dimitri Liakhovitski
2016-Jul-26 21:28 UTC
[R] removing all non-numeric characters from a string, but not "."
Hello! I have a string x: x <- c("x - 84", "y - 293.04", "z = 12.5") I want to remove all the non-numeric stuff from it. The following works: gsub("[^0-9]", "", x) However, it strips my numbers of "." Help - how could I do the same but leave the "." in? Thanks a lot! -- Dimitri Liakhovitski
Marc Schwartz
2016-Jul-26 21:39 UTC
[R] removing all non-numeric characters from a string, but not "."
> On Jul 26, 2016, at 4:28 PM, Dimitri Liakhovitski <dimitri.liakhovitski at gmail.com> wrote: > > Hello! > > I have a string x: > x <- c("x - 84", "y - 293.04", "z = 12.5") > > I want to remove all the non-numeric stuff from it. The following works: > gsub("[^0-9]", "", x) > > However, it strips my numbers of "." > > Help - how could I do the same but leave the "." in? > > Thanks a lot! > > -- > Dimitri Liakhovitski> gsub("[^0-9\\.]", "", x)[1] "84" "293.04" "12.5" The period needs to be escaped since it otherwise has special meaning in the regex. Regards, Marc Schwartz
David Winsemius
2016-Jul-26 21:40 UTC
[R] removing all non-numeric characters from a string, but not "."
> On Jul 26, 2016, at 2:28 PM, Dimitri Liakhovitski <dimitri.liakhovitski at gmail.com> wrote: > > gsub("[^0-9]", "", x)?regex I think you might be bit embarrassed because it seems pretty obvious once you know that character class elements like "." don't need to be escaped so it's just this:> gsub("[^0-9.]", "", x)[1] "84" "293.04" "12.5" You might want to add in some separator if you are processing expression this way.> gsub("[^0-9., ]", "", gsub( "[-+*/]", " , ", x) )[1] " , 84" " , 293.04 , 1200" " 12.5" -- David Winsemius Alameda, CA, USA
Marc Schwartz
2016-Jul-26 21:45 UTC
[R] removing all non-numeric characters from a string, but not "."
> On Jul 26, 2016, at 4:39 PM, Marc Schwartz <marc_schwartz at me.com> wrote: > > >> On Jul 26, 2016, at 4:28 PM, Dimitri Liakhovitski <dimitri.liakhovitski at gmail.com> wrote: >> >> Hello! >> >> I have a string x: >> x <- c("x - 84", "y - 293.04", "z = 12.5") >> >> I want to remove all the non-numeric stuff from it. The following works: >> gsub("[^0-9]", "", x) >> >> However, it strips my numbers of "." >> >> Help - how could I do the same but leave the "." in? >> >> Thanks a lot! >> >> -- >> Dimitri Liakhovitski > > >> gsub("[^0-9\\.]", "", x) > [1] "84" "293.04" "12.5" > > The period needs to be escaped since it otherwise has special meaning in the regex. > > Regards, > > Marc SchwartzActually, let me correct my reply. When in a character group, as is the case here, the period does not need to be escaped:> gsub("[^0-9.]", "", x)[1] "84" "293.04" "12.5" Regards, Marc
peter dalgaard
2016-Jul-26 21:48 UTC
[R] removing all non-numeric characters from a string, but not "."
> On 26 Jul 2016, at 23:28 , Dimitri Liakhovitski <dimitri.liakhovitski at gmail.com> wrote: > > Hello! > > I have a string x: > x <- c("x - 84", "y - 293.04", "z = 12.5") > > I want to remove all the non-numeric stuff from it. The following works: > gsub("[^0-9]", "", x) > > However, it strips my numbers of "." > > Help - how could I do the same but leave the "." in?How about this?> gsub("[^0-9.]", "", x)[1] "84" "293.04" "12.5" -pd> > Thanks a lot! > > -- > Dimitri Liakhovitski > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Dimitri Liakhovitski
2016-Jul-26 21:57 UTC
[R] removing all non-numeric characters from a string, but not "."
Thank you very much, gentlemen! On Tue, Jul 26, 2016 at 5:48 PM, peter dalgaard <pdalgd at gmail.com> wrote:> >> On 26 Jul 2016, at 23:28 , Dimitri Liakhovitski <dimitri.liakhovitski at gmail.com> wrote: >> >> Hello! >> >> I have a string x: >> x <- c("x - 84", "y - 293.04", "z = 12.5") >> >> I want to remove all the non-numeric stuff from it. The following works: >> gsub("[^0-9]", "", x) >> >> However, it strips my numbers of "." >> >> Help - how could I do the same but leave the "." in? > > How about this? > >> gsub("[^0-9.]", "", x) > [1] "84" "293.04" "12.5" > > -pd > >> >> Thanks a lot! >> >> -- >> Dimitri Liakhovitski >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. > > -- > Peter Dalgaard, Professor, > Center for Statistics, Copenhagen Business School > Solbjerg Plads 3, 2000 Frederiksberg, Denmark > Phone: (+45)38153501 > Office: A 4.23 > Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com > > > > > > > > >-- Dimitri Liakhovitski