[R] numbers as part of long character

Charilaos Skiadas cskiadas at gmail.com
Fri Jun 13 00:52:20 CEST 2008


On Jun 12, 2008, at 6:34 PM, Hua Li wrote:

> Thanks, Marc and Haris!
>
> I didn't know the values of the numbers beforehand, so the scan  
> method won't work, but "[^+-\\d.]+" will do!
>
> And Haris, I didn't intend to keep the information of which number  
> is B, which is C etc when asking the question, as I had a tedious  
> way to do it (use strspilt and unlist over and over again, after I  
> get the number). But if you have a easier way to do it, I'd like to  
> know!

Depending on how your real use case looks like, the following might  
work:

vec1 <- strsplit(outtree.new, "[^+-\\d.:\\w]+", perl=TRUE)[[1]]
nums <- as.numeric(gsub("\\w?:","", vec1, perl=TRUE))
names(nums) <- gsub(":[+-\\d.]+","", vec1, perl=TRUE)

If it doesn't, then provide us with the example that fails it.

> Hua

Haris Skiadas
Department of Mathematics and Computer Science
Hanover College



More information about the R-help mailing list