[R] extract integers from string

Henrique Dallazuanna wwwhsd at gmail.com
Thu May 12 21:13:40 CEST 2011


Try this:

library(gsubfn)
strapply(x, "\\d+", as.numeric, simplify = rbind)

On Thu, May 12, 2011 at 3:06 PM, Alon Honig <honeyoak at gmail.com> wrote:
> I have a vector with a long list of sentences that contain integers. I
> would like to extract the integers in a manner such that they are
> separate and manipulatable. for example:
> x[i] <- "sally has 20 dollars in her pocket and 3 marbles"
> x[i+1] <-  "30 days ago john had a 400k house"
>
> all sentences are different and contain a mixture of both integers and
> characters.
>
>  i would like to get a conditional matrix such that:
>
> y[i,j] <- 20    y[i,j+1] <- 3
> y[i+1,j] <- 30 y[i+1,j+1] <- 400
>
> based on some criteria (i.e. order, string length, keyword, etc...)
> the integers are sorted.
>
>
> most of my trouble is with finding the correct way to use gsub() or
> strsplit() such that the strings are integers that can be inputed into
> a matrix.
>
> thanks.
>
> ______________________________________________
> 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



More information about the R-help mailing list