[R] character to numeric conversion

Gabor Grothendieck ggrothendieck at gmail.com
Mon Mar 19 12:39:02 CET 2007


Here is one way.  This matches strings which contain those characters
found in a number, converting each such string to numeric.

library(gsubfn)
strapply(x, "[-0-9+.E]+", as.numeric)


On 3/19/07, Robin Hankin <r.hankin at noc.soton.ac.uk> wrote:
> Hi.
>
> Is there a straightforward way to convert a character string
> containing comma-delimited
> numbers  to a numeric vector?
>
> In my application, I use
>
> system(executable.string, intern=TRUE)
>
> which returns a string like
>
> "[0.E-38, 2.096751179214927596171268230,
> 3.678944959657480671183123052, 4.976528845643001020345216157,
> 6.072390165503099343887569007, 7.007958550337542210168866070,
> 7.807464185827177139302778736, 8.486139455817034846608029724,
> 9.053706780665060873259065771, 9.516172308326877463284426111,
> 9.876856047379733199590985269, 10.13695826383869052536062804,
> 10.29580989588667234885515374, 10.35092785255025551187463209,
> 10.29795676261278695909972578, 10.13052574735986793562227138,
> 9.839990935943625006580521345, 9.414977153151389385186358494,
> 8.840562526759586215404890348, 8.096830792651667245232639586,
> 7.156244887881612948153311800, 5.978569259122249264778017262,
> 4.499809670330265066808481929, 2.602689685444383764768503589, 0.E-38]"
>
>
> (the output is a single line).   In a big run, the string may contain
> 10^5 or possibly 10^6 numbers.
>
> What's the recommended way to convert this to a numeric vector?
>
>
>
>
>
>
> --
> Robin Hankin
> Uncertainty Analyst
> National Oceanography Centre, Southampton
> European Way, Southampton SO14 3ZH, UK
>  tel  023-8059-7743
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list