0

I have created a 300000 x 7 numeric matrix in R and I want to work with it in both R and Matlab. However, I'm not able to create a file well readeable for Matlab. When using the command save(), with file=xx.csv, it recognizes 5 columns instead; with extension .txt all data is opened in a single column instead. I have also tried with packages ff and ffdf to manage this big data (I guess the problem of R identifying rows and column when saving is related somehow to this), but I don't know how to save it in a readable format for Matlab afterwards.

An example of this dataset would be:

output <- matrix(runif(2100000, 1, 1000), ncol=7, nrow=300000)

  • 1
    Have you tried write.table(matrix, file = "csv_name.csv", sep=",")? I have already used it for files just as large. It is hard to know the problem without a reproducible example. – Lucas Fortini Jan 21 '14 at 17:36
  • 1
    `save` saves R objects in R's native binary format so I'm surprised you get anything out of that in Matlab. Changing the extension won't help. Read `help(save)` for info. – Spacedman Jan 21 '14 at 18:03
  • Did my answer solve your problem? If so, I'd appreciate it if you accepted it, or else I'll be happy to help out if there's more you need regarding your original question. – fotNelton Jan 22 '14 at 12:01

1 Answers1

4

If you want to work both with R and Matlab, and you have a matrix as big as yours, I'd suggest using the R.matlab package. The package provides methods readMat and writeMat. Both methods read/write the binary format that is understood by Matlab (and through R.matlab also by R).

Install the package by typing

install.packages("R.matlab")

Subsequently, don't forget to load the package, e.g. by

library(R.matlab)

The documentation of readMat and writeMat, accessible through ?readMat and ?writeMat, contains easy usage examples.

fotNelton
  • 3,844
  • 2
  • 24
  • 35
  • Yes, it worked perfectly. In fact, I was looking for the code that Lucas posted it. I tried before with sep=" " or "\t", but none of them worked for me. However, doing what you suggest I don't need to keep the data in a .csv file, so won't waste my time converting files. I'm extremely grateful for your post! – user3032330 Jan 22 '14 at 14:23
  • Using the binary format should be faster in this case anyway. If you don't mind, would you consider [accepting the answer](http://meta.stackexchange.com/questions/23138/how-to-accept-the-answer-on-stack-overflow)? ;-) – fotNelton Jan 22 '14 at 15:15