How can I read float data from binary using R
I know there is no float in R. So how can I read float data from binary. data structure 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);
the amount is not the correct value. how can i get the correct value as a float?
+3
source to share
1 answer
Your C structure consists of 5 integers followed by floats and then 2 integers. Thus, 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 are processing the value float
by setting the argument size
to 4
, since a float
is 4 bytes in size (instead of 8 of double
).
+3
source to share