Hello! First, I must also congratulate the R core team with their accomplishment! I have gotten to like R a lot, and I have recommended it for inclusion in an "Astronomy for Linux"-distribution which is in use by many professional astronomers and observatories. I'm currently working on importing data from files created by a C program (that I have not written myself, I have the source code, but I don't know if the author wants to publish it yet). I really don't know much about C, but I can understand that the program writes to a file using fwrite(MAP,sizeof(float),MAP_i*MAP_j,f) where MAP most probably is a pointer to a two-dimensional array of single precision floats. This is what I want to read into a matrix in R. When I look at the files written by this program, it is clear that they are not on a simple form, less says it is a binary file. However, it appears to be kind of common, as I have seen other packages (such as IDL) reading files like this without much ado... :-) So, can I read the data of these files into R? (If not, I may sit down and try to hack it up myself, some hints would be greatly appreciated). BTW, something unrelated, if anybody has a couple of examples of generating random numbers from distribution functions with a couple of jumps, they would like to share, I'd be happy (I'm an astronomer with very little training in statistics, so I'm uncertain whether I've got my code right (while it returns the expected results, I have a bad feeling)). Best, Kjetil -- Kjetil Kjernsmo Graduate astronomy-student Problems worthy of attack University of Oslo, Norway Prove their worth by hitting back E-mail: kjetikj at astro.uio.no - Piet Hein Homepage <URL:http://www.astro.uio.no/~kjetikj/> Webmaster at skepsis.no -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Tue, 29 Feb 2000, Kjetil Kjernsmo wrote:> I'm currently working on importing data from files created by a C program > (that I have not written myself, I have the source code, but I don't know > if the author wants to publish it yet). > > I really don't know much about C, but I can understand that the program > writes to a file using > fwrite(MAP,sizeof(float),MAP_i*MAP_j,f) > where MAP most probably is a pointer to a two-dimensional array of single > precision floats. This is what I want to read into a matrix in R.Just link a bit of C in to read the format with fread. You will need to do this anyway: R does not use the float type. Here's a bit of code we use for similar purposes. This reads 64x64 slices of 16 bit values from fMRI machine files. /* Read a slice from a .img file */ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include "R.h" void readimg(unsigned int *ans, int *nslice, char **name) { int fd, i; unsigned int t1, t2; char *p, buf[64*64*2]; long nread; fd = open(name[0], O_RDONLY); if(fd < 0) { PROBLEM "can't open file" RECOVER(NULL_ENTRY); } lseek(fd, 2*64*64*(*nslice), SEEK_SET); nread = read(fd, buf, 64 * 64*2); p = buf; for(i = 0; i < 64*64; i++) { t1 = (*p++)&0xFF; t2 = (*p++)&0xFF; *ans++ = 256 * t1 + t2; } close(fd); } called by getslice <- function (nslice, name = "MRI.img") .C("readimg", ans = integer(64 * 64), as.integer(nslice), name)$ans> When I look at the files written by this program, it is clear that they > are not on a simple form, less says it is a binary file. However, it > appears to be kind of common, as I have seen other packages (such as IDL) > reading files like this without much ado... :-) So, can I read the data of > these files into R? (If not, I may sit down and try to hack it up myself, > some hints would be greatly appreciated).Um. However do they know what it is (float, double, integer, dimensions).... Yes, they read it, often incorrectly! Alternatively, netpbm has lots of filters to dump such formats to ASCII. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Tue, 29 Feb 2000, Kjetil Kjernsmo wrote:> > I'm currently working on importing data from files created by a C program > (that I have not written myself, I have the source code, but I don't know > if the author wants to publish it yet). > > I really don't know much about C, but I can understand that the program > writes to a file using > fwrite(MAP,sizeof(float),MAP_i*MAP_j,f) > where MAP most probably is a pointer to a two-dimensional array of single > precision floats. This is what I want to read into a matrix in R. >We don't currently have any support for this in R. It's not that hard to do, but it doesn't generalise that well to eg multiple variables. What you want to do is to write a C function that accepts a filename and a pointer to a vector of doubles. It should open the file, read each float, cast it to double, and put it in the vector of doubles, then close the file. void readTheFile(char **filename, double *workspace) Calling this function from R would read the numbers into the supplied vector, and you could then fix up the dimensions in R (I'm assuming you know the size of the file here) a<-.C("readTheFile",filename,numeric(size.of.file)) m<-a[[2]] dim(m)<-c(100,200) #for example The manual on writing R Extensions should help. -thomas Thomas Lumley Assistant Professor, Biostatistics University of Washington, Seattle -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Reasonably Related Threads
- [RFC PATCH v3] virito-serial: A guest <-> host interface
- [RFC PATCH v3] virito-serial: A guest <-> host interface
- virtio-serial: An interface for host-guest communication
- virtio-serial: An interface for host-guest communication
- [PATCH] fix fwrite declaration