Re: [R] convert delimited strings with ranges to numeric

2013-08-14 Thread Bert Gunter
Better yet! -- Bert On Wed, Aug 14, 2013 at 6:03 PM, Richard M. Heiberger wrote: > I would use source > >> x<- "2,5-7,10,12-15" >> source(textConnection(paste("c(", gsub("\\-", ":", x), ")")))$value > [1] 2 5 6 7 10 12 13 14 15 > > > > On Wed, Aug 14, 2013 at 8:10 PM, Bert Gunter wrote: >>

Re: [R] convert delimited strings with ranges to numeric

2013-08-14 Thread arun
m: Bert Gunter To: arun Cc: Chris Stubben ; R help ; Marc Schwartz Sent: Wednesday, August 14, 2013 8:10 PM Subject: Re: [R] convert delimited strings with ranges to numeric Neither gsubfn nor eval(parse)) is required, of course: > x<-  "2,5-7,10,12-15" > z <-strsplit(sca

Re: [R] convert delimited strings with ranges to numeric

2013-08-14 Thread Richard M. Heiberger
I would use source > x<- "2,5-7,10,12-15" > source(textConnection(paste("c(", gsub("\\-", ":", x), ")")))$value [1] 2 5 6 7 10 12 13 14 15 On Wed, Aug 14, 2013 at 8:10 PM, Bert Gunter wrote: > Neither gsubfn nor eval(parse)) is required, of course: > > > x<- "2,5-7,10,12-15" > > z <-str

Re: [R] convert delimited strings with ranges to numeric

2013-08-14 Thread Bert Gunter
Neither gsubfn nor eval(parse)) is required, of course: > x<- "2,5-7,10,12-15" > z <-strsplit(scan(text=x,sep=",",wh="a"),split="-") ## use scan to vectorize > the string > l <- lapply(z,as.numeric) > unlist(lapply(l,function(x){ last <- x[length(x)]## alternately could use if() on the len

Re: [R] convert delimited strings with ranges to numeric

2013-08-14 Thread arun
Hi, May be this helps: library(gsubfn) as.numeric(strsplit(gsub("[c() ]","",gsubfn("([0-9]+)-([0-9]+)", ~as.numeric(seq(x,y)),x)),",")[[1]]) #[1]  2  5  6  7 10 12 13 14 15 A.K. - Original Message - From: Chris Stubben To: r-help@r-project.org Cc: Sent: Wednesday, August 14, 2013 1:41

Re: [R] convert delimited strings with ranges to numeric

2013-08-14 Thread Marc Schwartz
On Aug 14, 2013, at 12:41 PM, Chris Stubben wrote: > Is there an easy way to convert character strings with comma-separated > numbers and ranges to a numeric vector? > > x<- "2,5-7,10,12-15" > > [1] 2 5 6 7 10 12 13 14 15 > > Thanks, > Chris > There is a general admonishment to not