2015-09-26 72 views
0

我发现rmatio是一个很好的工具,可以将数据框和列表转换为.mat文件(尽管看起来该字符不受支持),但在下面的演示,我想生成一个名为data_list的嵌套列表,但是当我使用write.mat将其导出为.mat文件时,只保留第一列数据,请参阅如何解决此类问题?提前致谢。如何将嵌套列表转换为matlab中的结构(.mat)

library(rmatio) 
data(iris) 
iris 
iris$group<-paste("group",rep(1:2,75),sep="") 

names(iris)<-c('a','b','c','d','species','group') 
## "."is not support as name in .mat file, so I rename it as "a b c d" for simplicity. 
head(iris) 
##split the data into list according to species. 
out<-split(iris[,c(1:4,6)],f=iris$species) 
out 
##further split the data by group variable to create a nest list. 
data_list<-lapply(out,function(x){ 
split(x,f=x$group) 
}) 

write.mat(data_list, 'iris.mat') 

回答

1

安装和装载倍频和学习了几个命令,并与write.mat功能试验后,我的结论是,它不处理嵌套列表。它显然将列表中的每个项目写入matlab文件中的单独变量。所以,如果你希望所有的虹膜数据(分成两组),以进入一个matlab文件,并与你需要像这样它信息写入单独的变量结束:在一个八度会议

iris$group <- 1:2 
data_list <- 
split(unlist(iris[1:4]), f=interaction(iris$Species, iris$group, rep(names(iris)[1:4], each=150))) 
str(data_list) 
write.mat(data_list, 'iris2.mat') 

然后你看:

octave-3.4.0:19> load /Users/davidwinsemius/iris2.mat 
octave-3.4.0:20> whos 
Variables in the current scope: 

    Attr Name       Size      Bytes Class 
    ==== ====       ====      ===== ===== 
     setosa.1.Petal.Length   1x25      200 double 
     setosa.1.Petal.Width   1x25      200 double 
     setosa.1.Sepal.Length   1x25      200 double 
     setosa.1.Sepal.Width   1x25      200 double 
     setosa.2.Petal.Length   1x25      200 double 
     setosa.2.Petal.Width   1x25      200 double 
     setosa.2.Sepal.Length   1x25      200 double 
     setosa.2.Sepal.Width   1x25      200 double 
     versicolor.1.Petal.Length  1x25      200 double 
     versicolor.1.Petal.Width  1x25      200 double 
     versicolor.1.Sepal.Length  1x25      200 double 
     versicolor.1.Sepal.Width  1x25      200 double 
     versicolor.2.Petal.Length  1x25      200 double 
     versicolor.2.Petal.Width  1x25      200 double 
     versicolor.2.Sepal.Length  1x25      200 double 
     versicolor.2.Sepal.Width  1x25      200 double 
     virginica.1.Petal.Length  1x25      200 double 
     virginica.1.Petal.Width  1x25      200 double 
     virginica.1.Sepal.Length  1x25      200 double 
     virginica.1.Sepal.Width  1x25      200 double 
     virginica.2.Petal.Length  1x25      200 double 
     virginica.2.Petal.Width  1x25      200 double 
     virginica.2.Sepal.Length  1x25      200 double 
     virginica.2.Sepal.Width  1x25      200 double 

Total is 600 elements using 4800 bytes 

有一个R.matlab -package是没有数据帧虹膜与少巴洛克转移过程:

> require(R.matlab) 
Loading required package: R.matlab 
R.matlab v3.2.0 (2015-02-24) successfully loaded. See ?R.matlab for help. 

Attaching package: ‘R.matlab’ 

The following objects are masked from ‘package:base’: 

    getOption, isOpen 

> writeMat("iris3.mat", iris=iris, matVersion="5", onWrite=NULL, verbose=FALSE) 

内调单OBJE与6列命名。它也能够将data_list结构作为一个具有24列的单个项目。

octave-3.4.0:35> load /Users/davidwinsemius/dl.mat 
octave-3.4.0:36> whos 
Variables in the current scope: 

    Attr Name   Size      Bytes Class 
    ==== ====   ====      ===== ===== 
     data_list  1x1      4800 struct 
     iris   1x1      4800 struct 

Total is 2 elements using 9600 bytes 
+0

很好,它的工作原理!尽管不支持嵌套列表,但它仍然可以工作。我已经在matlab上测试了脚本,唯一的不兼容是matlab中不支持点(“。”)作为变量名的一部分,因此需要添加一行来相应地修改名称(名称(data_list)< - gsub(“\\。”,“_”,names(data_list)))。 – johnsonzhj

相关问题