[R] converting a character string to a subscripted numeric variable
Sundar Dorai-Raj
sundar.dorai-raj at pdf.com
Thu Nov 10 23:00:21 CET 2005
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