[R] Spliting a text

Gabor Grothendieck ggrothendieck at gmail.com
Wed Aug 4 13:34:15 CEST 2010


On Wed, Aug 4, 2010 at 7:04 AM, Megh Dal <megh700004 at yahoo.com> wrote:
> Hi, I want to split a text to seperate numerical and non-numerical portions of that. For example suppose I have a text "abc 3456" and I want to split in 2 parts like "abc" & "3456".
>
> Is there any function to do that?
>

If the parts of your data are separated by a space then you could use
strsplit or read.table

> x <- "abc 3456"
>
> strsplit(x, " ")[[1]]
[1] "abc"  "3456"
>
> read.table(textConnection(x))
   V1   V2
1 abc 3456

If there is no separator try strapply in gsubfn:

> y <- "abc3456"
> library(gsubfn)
> strapply(y, "[^0-9]*|[0-9]*", c)[[1]]
[1] "abc"  "3456"



More information about the R-help mailing list