[R] converting a character string to a subscripted numeric variable

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Nov 10 23:23:44 CET 2005


"Charles Annis, P.E." <Charles.Annis at statisticalengineering.com> writes:

> Eternal gratitude to Sunbar and Matt and Patrick!
> 
> The easy solution is
> 
> Beta <- as.numeric(strsplit(input.line [7], ",")[[1]])
> Beta <- Beta[!is.na(Beta)]
> Beta

Also

x <- input.line [7]

eval(parse(text=paste("c(", x, ")")))
 
or 

x <- sub(", *$","",x)
scan(textConnection(x), sep=",")


> I have a slew of files to interrogate and need to know from some of the
> input, what to look for in the remainder of the input.
> 
> Thanks to all!
> 
> 
> Charles Annis, P.E.
> 
> Charles.Annis at StatisticalEngineering.com
> phone: 561-352-9699
> eFax:  614-455-3265
> http://www.StatisticalEngineering.com
>  
> -----Original Message-----
> From: Sundar Dorai-Raj [mailto:sundar.dorai-raj at pdf.com] 
> Sent: Thursday, November 10, 2005 5:00 PM
> To: Charles.Annis at statisticalengineering.com
> Cc: R-help at r-project.org
> Subject: Re: [R] converting a character string to a subscripted numeric
> variable
> 
> 
> 
> Charles Annis, P.E. wrote:
> > Dear R-helpers:
> > 
> > It seems that I have a mental block.  (Some say that it sits atop my
> > shoulders.)
> > 
> > For reasons too tedious to retell I have an R object:
> > 
> > 
> >>input.line[7]
> > 
> > [1] "-13.24, -11.24, -9.24, -7.24, -5.24, -3.24, -1.24, 0.76, 2.76, 4.76,
> > 6.76, 8.76, 10.76, 12.76, 14.76, 16.76, 18.76, 20.76, 22.76, 24.76, 26.76,
> > 28.76, 30.76, 32.76, 34.76, 36.76, 38.76, 40.76, 42.76, 44.76, "
> > 
> > 
> > I would like to convert this into a subscripted variable, Beta, something
> > that should be straightforward if I had *almost* what I have.
> > 
> > I'd like to say
> > 
> > Beta <- c(-13.24, -11.24, -9.24, -7.24, -5.24, -3.24, -1.24, 0.76, 2.76,
> > 4.76, 6.76, 8.76, 10.76, 12.76, 14.76, 16.76, 18.76, 20.76, 22.76, 24.76,
> > 26.76, 28.76, 30.76, 32.76, 34.76, 36.76, 38.76, 40.76, 42.76, 44.76)
> > 
> > but I can't because:
> > 
> > input.line[7] is a character string, and
> > 
> > it ends in a comma.
> > 
> > 
> > This cannot be as difficult as I have found it to be.  Can anyone help?
> > 
> > Copious Thanks.
> 
> How about:
> 
> x <- "-13.24, -11.24, "
> x <- as.numeric(strsplit(x, ",")[[1]])
> x[!is.na(x)]
> 
> --sundar
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907




More information about the R-help mailing list