Re: [R] reading short int and float from binary connection
Angel Lopez wrote: Thanks for the advise. I think I've found the solution, the problem was that the file had been written from a C program using a structure containing floats and int, if I rewrite the C code not using a structure but independent floats and ints the data then is read smoothly into R. I still can't figure out why a structure is different or how should I have read it into R but it is probably more a C than an R question . You're probably right there. A C structure may be silently aligned by the compiler so that value fields are not contiguous in memory. A binary write of such a structure cannot be assumed to have bytes in the same order described in the structure definition. Jim __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] reading short int and float from binary connection
Hi, I have a binary file with mixture of short integers (2 bytes) and floats (4 bytes). To get the data into R I use readBin reading each value one at a time (n=1) but it is giving me headaches, two questions: -In the what option is there any difference between integer and int? -To read the short int I use what=int and size=2 and to read the floats I use what=double and size=4, the strange thing is that sometimes it works but other times it reads wrong values. Any clues where my mistake might be? Thanks Angel __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] reading short int and float from binary connection
Angel Lopez wrote: Hi, I have a binary file with mixture of short integers (2 bytes) and floats (4 bytes). To get the data into R I use readBin reading each value one at a time (n=1) but it is giving me headaches, two questions: -In the what option is there any difference between integer and int? -To read the short int I use what=int and size=2 and to read the floats I use what=double and size=4, the strange thing is that sometimes it works but other times it reads wrong values. Any clues where my mistake might be? Thanks Angel Are the inetgers signed or not? You might want to set signed = FALSE for unsigned integers. Are you sure you are reading each time from the beginning of the connection? Are you really sure about the file format specifications? Difficult to track down without your code, the file format's specification, and an example file.. Uwe Ligges __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] reading short int and float from binary connection
Thanks for the advise. I think I've found the solution, the problem was that the file had been written from a C program using a structure containing floats and int, if I rewrite the C code not using a structure but independent floats and ints the data then is read smoothly into R. I still can't figure out why a structure is different or how should I have read it into R but it is probably more a C than an R question . Thanks, Angel Uwe Ligges wrote: Angel Lopez wrote: Hi, I have a binary file with mixture of short integers (2 bytes) and floats (4 bytes). To get the data into R I use readBin reading each value one at a time (n=1) but it is giving me headaches, two questions: -In the what option is there any difference between integer and int? -To read the short int I use what=int and size=2 and to read the floats I use what=double and size=4, the strange thing is that sometimes it works but other times it reads wrong values. Any clues where my mistake might be? Thanks Angel Are the inetgers signed or not? You might want to set signed = FALSE for unsigned integers. Are you sure you are reading each time from the beginning of the connection? Are you really sure about the file format specifications? Difficult to track down without your code, the file format's specification, and an example file.. Uwe Ligges . __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html