[R] Bytes to Numeric/Float conversion
Duncan Murdoch
murdoch.duncan at gmail.com
Wed Jun 12 13:20:28 CEST 2013
On 13-06-12 6:54 AM, Bikash Agrawal wrote:
> Actually I am using rJava, running some map-reduce using R.
> The data send is in bytes, but actually it is floating-point number.
>
> On R side, I need to convert this byte into Float. But I couldn't find any
> function.
> I am try using rawToChar() which convert into character. And later on
> as.double to convert it.
>
> But it is not giving the exact value.
> Could any one help me out.
>
If you want exact transfer from bytes, use raw connections. For example:
> con <- rawConnection(raw(0), "r+")
> writeBin(pi, con)
> rawConnectionValue(con)
[1] 18 2d 44 54 fb 21 09 40
> seek(con,0)
[1] 8
> readBin(con, "numeric")
[1] 3.141593
Depending on how you created the bytes, you may need to do some fiddling
with the optional parameters size and endian of readBin to read them
properly.
Duncan Murdoch
More information about the R-help
mailing list