Re: [R] Moving data between R and Matlab back and forth?

From: Tribo Laboy <tribolaboy_at_gmail.com>
Date: Thu, 27 Mar 2008 16:26:14 +0900

Hello,

Thanks for the input. I thought it wasn't much fun to talk to myself on the public forums. ;-)

Trying the line you suggested generated and error:

as.data.frame(drop(labpcimport))

Error in data.frame(Maker = list("HP", "HP", "Sony", "DELL", "whitebox", :   arguments imply differing number of rows: 1, 6

However I think I found how to do it. Here is the snippets that works for me.

--------------------------------Code begin ---------------

labpc <- drop(labpcimport) # Drops all the unnecessary indexes with length 1 and the list becomes much simpler

# If any of the contents of each cell is a list then "rbind" its elements into a single vector and treat it as a factor # These elements were represented as cell elements of a cell array in the MATLAB structure.
for (k in 1:length(labpc)){

    if (mode(labpc[[k]]) == "list") {

        labpc[[k]]<- as.factor(do.call("rbind", labpc[[k]]))     }
}

labpcdata <- as.data.frame(labpc)

-----------------------------------Code end --------------------


Regards,

TL

On Thu, Mar 27, 2008 at 4:08 PM, Prof Brian Ripley <ripley_at_stats.ox.ac.uk> wrote:
> On Thu, 27 Mar 2008, Tribo Laboy wrote:
>
> > I realized that not everyone has Matlab and that basically the issue
> > is purely how to deal with the returned data in R, so I have revised
> > my example code and made it easier to copy-paste and run:
>
> Only for those with matlab! The rest of us have little clue what the
> format of the output is -- it looks like a list array, which is not what
> the help page for readMat says it is.
>
> I would try
>
> as.data.frame(drop(labpcimport))
>
>
>
>
> > #Make a data frame in R
> >
> > Maker <- factor(c("HP", "HP", "Sony", "DELL", "whitebox", "whitebox"))
> > CPUspeed <- c(2,4,2.5,2.5,2,5)
> > HDD <- c(80, 250, 100, 100, 80, 300)
> > RAM <- c(2, 2, 1, 2, 2, 4)
> > labpc <- data.frame(Maker, CPUspeed, HDD, RAM)
> > labpc
> >
> > #Save in Matlab v6 format with 'writeMat'
> >
> > library(R.matlab)
> > writeMat("labpc.mat", labpcexport = labpc)
> >
> > #Load the file in R with 'readMat'
> >
> > labpcfile <- readMat("labpc.mat")
> > labpcimport <- labpcfile$labpcexport
> > labpcimport
> >
> > # This is the last line output
> > #, , 1
> > #
> > # [,1]
> > #Maker List,6
> > #CPUspeed Numeric,6
> > #HDD Numeric,6
> > #RAM Numeric,6
> >
> > Now, how do I convert the result held in labpcimport back to a data frame?
> >
> > Thanks in advance,
> >
> > TL
> >
> > On Thu, Mar 27, 2008 at 1:27 AM, Tribo Laboy <tribolaboy_at_gmail.com> wrote:
> >> Hi to the list,
> >>
> >> I am trying to find a way to painlessly move structured data back and
> >> forth between R and Matlab (also Octave). For this purpose I found the
> >> R.matlab package great help. I wish to use a Matlab -v6 MAT file as an
> >> intermediary format, because it is well read by both Matlab and
> >> Octave. It is also well read by 'readMat' function in R.matlab
> >> package, but that is where I run into problems because of poor
> >> knowledge of R.
> >>
> >> By structured data I mean data in data frames in R and the closest
> >> equivalent - structures in Matlab. Here is what I have done.
> >>
> >> -----------------------------------------------------
> >> Make a data frame in R and export it
> >> -----------------------------------------------------
> >>
> >> > Maker <- factor(c("HP", "HP", "Sony", "DELL", "whitebox", "whitebox"))
> >> > CPUspeed <- c(2,4,2.5,2.5,2,5)
> >> > HDD <- c(80, 250, 100, 100, 80, 300)
> >> > RAM <- c(2, 2, 1, 2, 2, 4)
> >> > labpc <- data.frame(Maker, CPUspeed, HDD, RAM)
> >>
> >> > labpc
> >> Maker CPUspeed HDD RAM
> >> 1 HP 2.0 80 2
> >> 2 HP 4.0 250 2
> >> 3 Sony 2.5 100 1
> >> 4 DELL 2.5 100 2
> >> 5 whitebox 2.0 80 2
> >> 6 whitebox 5.0 300 4
> >>
> >> > library(R.matlab)
> >> > writeMat("labpc.mat", labpcdata = labpc)
> >> --------------------------------------------------------------
> >>
> >> --------------------------------------------------------------
> >> In MATLAB - everything is as expected
> >> --------------------------------------------------------------
> >> load('labpc.mat')
> >>
> >> >> labpcdata
> >>
> >> labpcdata =
> >>
> >> Maker: {6x1 cell}
> >> CPUspeed: [6x1 double]
> >> HDD: [6x1 double]
> >> RAM: [6x1 double]
> >>
> >> >> class(labpcdata)
> >>
> >> ans =
> >>
> >> struct
> >>
> >> >> labpcstruct = labpcdata
> >> >> save('labpcstruct.mat', 'labpcstruct')
> >> ---------------------------------------------------------
> >>
> >>
> >> -------------------------------------------------------
> >> Back in R - how to rebuild the data frame from the list labpcstruct?
> >> -------------------------------------------------------
> >> > labpcfile <- readMat("labpcstruct.mat")
> >> > labpcfile
> >> $labpcstruct
> >> , , 1
> >>
> >> [,1]
> >> Maker List,6
> >> CPUspeed Numeric,6
> >> HDD Numeric,6
> >> RAM Numeric,6
> >>
> >>
> >> attr(,"header")
> >> attr(,"header")$description
> >> [1] "MATLAB 5.0 MAT-file, Platform: PCWIN, Created on: Wed Mar 26
> >> 15:49:21 2008 "
> >>
> >> attr(,"header")$version
> >> [1] "5"
> >>
> >> attr(,"header")$endian
> >> [1] "little"
> >>
> >> > labpcstruct <- labpcfile$labpcstruct
> >> > labpcstruct
> >> , , 1
> >>
> >> [,1]
> >> Maker List,6
> >> CPUspeed Numeric,6
> >> HDD Numeric,6
> >> RAM Numeric,6
> >>
> >>
> >> > typeof(labpcstruct)
> >> [1] "list"
> >>
> >> --------------------------------------------
> >>
> >> So if there is any kind soul that will tell me how to get back the
> >> original data frame from the imported list 'labpcstruct', that would
> >> be great.
> >>
> >> Regards,
> >>
> >> TL
> >>
> >
> > ______________________________________________
> > R-help_at_r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
> --
> 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 272866 (PA)
> Oxford OX1 3TG, UK Fax: +44 1865 272595
>



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 27 Mar 2008 - 10:42:26 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 27 Mar 2008 - 11:30:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive