[R] extract integers from string

Alon Honig honeyoak at gmail.com
Thu May 12 20:06:15 CEST 2011


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.



More information about the R-help mailing list