[R] How to convert a character string to a number
Stephen D. Weigand
weigand.stephen at charter.net
Tue May 24 06:20:39 CEST 2005
Dear Andreas,
On May 23, 2005, at 4:56 AM, Kraft, Andreas wrote:
> Hi everyone,
> I have a simple problem but don´t know how to solve it.
> I read in a table from a text file that looks like that:
>
> -3,932200E-01 -2,000000E-01 4,999995E-02
> -3,932099E-01 -1,000000E-01 3,999996E-02
...
> -3,931300E-01 0,000000E+00 3,499996E-02
>
> but when I try to to plot the values from the
> second column, the plot shows the wrong values.
>
> That's what I do in R:
> read.table("C:\\Messdaten\\epp1_1_ver.txt")->sh1
>
Read the help for read.table() and use dec = ",".
Also, before you correct the call to read.table, if you enter
R> str(sh1)
you'll see something like:
R> str(sh1)
`data.frame': 10 obs. of 3 variables:
$ V1: Factor w/ 10 levels "-3,931300E-01",..: 10 9 8 7 6 5 4 3 2 1
$ V2: Factor w/ 5 levels "-1,000000E-01",..: 2 1 4 1 1 5 3 4 4 4
$ V3: Factor w/ 5 levels "2,499996E-02",..: 5 3 3 4 4 2 3 1 1 2
which gives you some very helpful information. In particular, it
shows that sh1 consists of factors.
Good luck,
Stephen
>> sh1[1:10,2]
>
> [1] -2.000000E-01 -1.000000E-01 0.000000E+00 -1.000000E-01
> -1.000000E-01 3.000000E-01 -3.000000E-01 0.000000E+00
>
> [9] 0.000000E+00 0.000000E+00
>
> 1707 Levels: -1,000000E-01 -1,000000E+00 -1,000000E+01 -1,002000E+02
> -1,003000E+02 -1,004000E+02 ... 9,980000E+01
>
>> sh1[1:10,2]->a
>
>> cbind(x=seq(1,10,by=1),y=a)
>
> x y
>
> [1,] 1 436
>
> [2,] 2 321
>
> [3,] 3 1007
>
> [4,] 4 321
>
> [5,] 5 321
>
> [6,] 6 1353
>
> [7,] 7 548
>
> [8,] 8 1007
>
> [9,] 9 1007
>
> [10,] 10 1007
>
>
>
> Why do I get those values? Why is for example -2,000000E-01 equal to
> 436?
>
> I need to convert the values from -2,000000E-01 to -0.2 - how can I
> do this?
>
>
>
> Thank you for your suggestions,
>
>
>
> Andreas Kraft
>
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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
>
More information about the R-help
mailing list