Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read float data from a binary file using R

Tags:

r

binary

I know there is no float in R. so How can I read float data from a binary file. the struct of data in C as follows

typedef struct
{
    int date;
    int open;
    int high;
    int low;
    int close;
    float amount;
    int vol;
    int reservation;
} StockData; 

to.read = file(filename, "rb");
line1=readBin(to.read,  "int",8);

amount is not the right value. how can I get the right value as float?

like image 759
Anguslilei Avatar asked Feb 21 '26 14:02

Anguslilei


1 Answers

Your C structure is made of 5 integer values followed by a float and then 2 integers again. So, you can call readBin three times:

 line1<-c(readBin(to.read,"int",5), 
          readBin(to.read,"double",1,size=4),
          readBin(to.read,"int",2))

You handle the float value by setting the size argument to 4, since a float has a size of 4 bytes (instead of the 8 of a double).

like image 51
nicola Avatar answered Feb 24 '26 04:02

nicola