Dear all,
I have a vector, which looks about like this:
str <- c("14.XYZ", "15.ABCDE", "16.dkieowo",
"120.EIDKAI")
I need to extract the numerical characters out of the string, so I receive
in the end a normal vector containing:
vec <- c(14, 15, 16, 120)
I need a generic solution for this. My actual vector contains about 403
strings, and it is bound to change with different versions of my
calculations. So I need something that either extracts all numeric
characters or deletes everything else.
I hope you can help me out.
Best,
Karl
[[alternative HTML version deleted]]
Hi Karl,
both strategies are possible using gsub:
gsub("([0-9]*).*","\\1",str) #extract numbers
gsub("[^0-9]","",str) #remove all non-numeric characters
wrap it nicely in an "as.numeric" call to get numeric values.
cheers.
Am 16.09.2011 11:13, schrieb Karl Weinmayer:> Dear all,
>
>
>
> I have a vector, which looks about like this:
>
>
>
> str <- c("14.XYZ", "15.ABCDE",
"16.dkieowo", "120.EIDKAI")
>
>
>
> I need to extract the numerical characters out of the string, so I receive
> in the end a normal vector containing:
>
>
>
> vec <- c(14, 15, 16, 120)
>
>
>
> I need a generic solution for this. My actual vector contains about 403
> strings, and it is bound to change with different versions of my
> calculations. So I need something that either extracts all numeric
> characters or deletes everything else.
>
>
>
> I hope you can help me out.
>
>
>
> Best,
>
> Karl
>
>
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
--
Eik Vettorazzi
Department of Medical Biometry and Epidemiology
University Medical Center Hamburg-Eppendorf
Martinistr. 52
20246 Hamburg
T ++49/40/7410-58243
F ++49/40/7410-57790
--
Pflichtangaben gem?? Gesetz ?ber elektronische Handelsregister und
Genossenschaftsregister sowie das Unternehmensregister (EHUG):
Universit?tsklinikum Hamburg-Eppendorf; K?rperschaft des ?ffentlichen Rechts;
Gerichtsstand: Hamburg
Vorstandsmitglieder: Prof. Dr. J?rg F. Debatin (Vorsitzender), Dr. Alexander
Kirstein, Joachim Pr?l?, Prof. Dr. Dr. Uwe Koch-Gromus
try this:> str <- c("14.XYZ", "15.ABCDE", "16.dkieowo", "120.EIDKAI") > x <- gsub("[^0-9]", "", str) > x[1] "14" "15" "16" "120"> as.integer(x)[1] 14 15 16 120>On Fri, Sep 16, 2011 at 5:13 AM, Karl Weinmayer <karl.weinmayer at gmx.at> wrote:> Dear all, > > > > I have a vector, which looks about like this: > > > > str <- c("14.XYZ", "15.ABCDE", "16.dkieowo", "120.EIDKAI") > > > > I need to extract the numerical characters out of the string, so I receive > in the end a normal vector containing: > > > > vec <- c(14, 15, 16, 120) > > > > I need a generic solution for this. My actual vector contains about 403 > strings, and it is bound to change with different versions of my > calculations. So I need something that either extracts all numeric > characters or deletes everything else. > > > > I hope you can help me out. > > > > Best, > > Karl > > > > > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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 Data Munger Guru What is the problem that you are trying to solve?