Re: [R] extract integers from string

From: Henrique Dallazuanna <wwwhsd_at_gmail.com>
Date: Thu, 12 May 2011 16:13:40 -0300

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

______________________________________________
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.
Received on Thu 12 May 2011 - 19:25:37 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 12 May 2011 - 19:30:06 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive