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

From: <Richard.Cotton_at_hsl.gov.uk>
Date: Thu, 27 Mar 2008 09:21:44 +0000


> > > 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:

> > > #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?

This works, but I don't know how well it will generalise to other imported matrices.
as.data.frame(lapply(drop(labpcimport), unlist))

Regards,
Richie.

Mathematical Sciences Unit
HSL



ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}

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 - 09:24:10 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 - 09:30:24 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