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? Thanks,
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"
On Aug 4, 2010, at 7:04 AM, Megh Dal 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??strsplit ?regex -- David Winsemius, MD West Hartford, CT
----- Original Message ----> From: Megh Dal <megh700004 at yahoo.com> > To: r-help at stat.math.ethz.ch > Sent: Wed, August 4, 2010 4:04:06 AM > Subject: [R] Spliting a text > > 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? > > Thanks,> full.string <- "abc 3456" > letter.part <- sub("^([[:alpha:]]+).*$", "\\1", full.string); letter.part[1] "abc"> numeric.part <- sub("^[^0-9]+(.*)$", "\\1", full.string); numeric.part[1] "3456">