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

Richard.Cotton at hsl.gov.uk Richard.Cotton at hsl.gov.uk
Thu Mar 27 10:21:44 CET 2008


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



More information about the R-help mailing list