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

Charles Annis, P.E. Charles.Annis at StatisticalEngineering.com
Thu Nov 10 23:10:40 CET 2005


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

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




More information about the R-help mailing list