Quantcast

uncoerce.... to get real number instead of integer?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

uncoerce.... to get real number instead of integer?

Nicole Barberis
Dear R community,
> #I'm using scan() to extract a real number from a file with a complex structure: 156689504.378.
> #My problem is that the number is coerced into an integer. 
> #Do you have any ideas for me on how to "uncoerce"?  Here is my code and the output:
> #I'm using R version 2.15.0 (2012-03-30)
>
> net3 <- patient.net3 <- scan("C:/temp/samplePHIoutput.txt", skip=23, what=list(character(0), character(0), numeric(0)))
Read 1 record
> net3[3]
[[1]]
[1] 156689504

>
> #I need the full number in net[3] and I need the output in vector form, so I used unlist().
> workfile <- unlist(net3)
>
> workfile[3]
[1] "156689504.378"
>
> #Just checking character length because I was curious.
> nchar(workfile[3])
[1] 13
>
> #convert character string to a number.
> number <- as.numeric(workfile[3])
>
> mode(number)
[1] "numeric"
> nchar(number)
[1] 13
> number
[1] 156689504
>
>
> #Notice that my original number is Real.   I read that the function as.numeric() coerces the output to integer.
> #2 questions:
> #  1) How can I uncoerce it?
> #  2) Why does nchar() still see 13 characters but output only 9?

Thank you.
-Nicky

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: uncoerce.... to get real number instead of integer?

Rui Barradas
Hello,

Don't worry, it's just a print thing. Just check this example:


x <- 123456789.0378
x
[1] 123456789
x - 123456789
[1] 0.0378


As you can see, it is a real. And as.numeric does NOT coerce to integer,
'numeric' is identical to 'double'. See the help page, ?as.numeric.

And for your number,

y <- "156689504.0378"
y <- as.numeric(y)
y
[1] 156689504
y - 156689504
[1] 0.03780001402

The extra decimals are due to the internal binary representation.
 From the help page for 'numeric':

"as.numeric is a generic function, but S3 methods must be written for
as.double. It is identical to as.double (and as.real)."

Hope this helps,

Rui Barradas

Em 24-06-2012 05:20, Nicole Barberis escreveu:

> Dear R community,
>> #I'm using scan() to extract a real number from a file with a complex structure: 156689504.378.
>> #My problem is that the number is coerced into an integer.
>> #Do you have any ideas for me on how to "uncoerce"?  Here is my code and the output:
>> #I'm using R version 2.15.0 (2012-03-30)
>>
>> net3 <- patient.net3 <- scan("C:/temp/samplePHIoutput.txt", skip=23, what=list(character(0), character(0), numeric(0)))
> Read 1 record
>> net3[3]
> [[1]]
> [1] 156689504
>
>>
>> #I need the full number in net[3] and I need the output in vector form, so I used unlist().
>> workfile <- unlist(net3)
>>
>> workfile[3]
> [1] "156689504.378"
>>
>> #Just checking character length because I was curious.
>> nchar(workfile[3])
> [1] 13
>>
>> #convert character string to a number.
>> number <- as.numeric(workfile[3])
>>
>> mode(number)
> [1] "numeric"
>> nchar(number)
> [1] 13
>> number
> [1] 156689504
>>
>>
>> #Notice that my original number is Real.   I read that the function as.numeric() coerces the output to integer.
>> #2 questions:
>> #  1) How can I uncoerce it?
>> #  2) Why does nchar() still see 13 characters but output only 9?
>
> Thank you.
> -Nicky
>
> [[alternative HTML version deleted]]
>
>
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: uncoerce.... to get real number instead of integer?

Petr Savicky
On Sun, Jun 24, 2012 at 07:29:23AM +0100, Rui Barradas wrote:

> Hello,
>
> Don't worry, it's just a print thing. Just check this example:
>
>
> x <- 123456789.0378
> x
> [1] 123456789
> x - 123456789
> [1] 0.0378

Hello:

If you want to see more digits of the number, use one of the
following.

  print(x, digits=17)

  [1] 123456789.0378

  options(digits=17)
  x

  [1] 123456789.0378

The default setting of options("digits") is 7.

Hope this helps.

Petr Savicky.

______________________________________________
[hidden email] 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.
Loading...