我有一个数组,使用readMat()从matlab导入。
数组的大小为X,1,1。
每个元素都是另一个数组,依此类推。它像嵌套列表一样分支出来。
现在阵列的每个级别都看起来像这样:
> Array
, , 1
[,1]
f.name "file.EDF"
CNT List,4
> Array[2] #This is CNT
[[1]]
, , 1
[,1]
FileType "EDF"
CntGen List,22
CntEle List,14
CntEve List,6
我想将它转换为嵌套列表,我已经完成了这一堆for循环:
#Removes dimensons by converting to a vector
asVec <- function(X){
t <- lapply(X, as.vector)
names(t) <- dimnames(X)[[1]]
t
}
#Convert arrays to lists
GetAna <- function(ana){
ana<-asVec(ana)
for(i in 1:length(ana)){ #loops through the datastructure converting one branch at a time
if(length(dim(ana[[i]]))>1){
ana[[i]]<-asVec(ana[[i]])
for(j in 1:length(ana[[i]])){
if(length(dim(ana[[c(i,j)]]))>1){
ana[[c(i,j)]]<-asVec(ana[[c(i,j)]])
for(k in 1:length(ana[[c(i,j)]])){
if(length(dim(ana[[c(i,j,k)]]))>1){
ana[[c(i,j,k)]]<-asVec(ana[[c(i,j,k)]])
}
}
}
}
}
}
ana #returns ana
}
这将返回如下列表:
> list(f.name = "File.EDF",
+ CNT = list(FileType = "EDF",
+ CntGen = list(time = "12.45.21",
+ date = "28.01.15"),
+ ECG = "vector of ECG values"))
$f.name
[1] "File.EDF"
$CNT
$CNT$FileType
[1] "EDF"
$CNT$CntGen
$CNT$CntGen$time
[1] "12.45.21"
$CNT$CntGen$date
[1] "28.01.15"
$CNT$ECG
[1] "vector of ECG values"
这很有效,但显然是一团糟。
有更有效的方法吗?
数据样本的输入:
list(structure(list(list(structure(list(structure("ECG", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("Accelerometer_X", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("Accelerometer_Y", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("Accelerometer_Z", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("Marker", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("HRV", .Dim = c(1L,
1L))), .Names = "")), list(structure(list(structure("ECG electrode", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("X-axis", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("Y-axis", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("Z-axis", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("Pushbutton marker", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("Heart Rate Variability", .Dim = c(1L,
1L))), .Names = "")), list(structure(list(structure("uV", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("mg", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("mg", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure("mg", .Dim = c(1L,
1L))), .Names = ""), structure(list(structure(character(0), .Dim = 0:1)), .Names = ""),
structure(list(structure("ms", .Dim = c(1L, 1L))), .Names = "")),
structure(c(-32768, -4045, -4017, -3877, -32768, 0), .Dim = c(1L,
6L)), structure(c(32767, 4045, 4017, 3877, 32767, 65535), .Dim = c(1L,
6L)), structure(c(-32768, -8192, -8192, -8192, -32768, -32768
), .Dim = c(1L, 6L)), structure(c(32767, 8192, 8192, 8192,
32767, 32767), .Dim = c(1L, 6L)), list(structure(list(structure(character(0), .Dim = 0:1)), .Names = ""),
structure(list(structure(character(0), .Dim = 0:1)), .Names = ""),
structure(list(structure(character(0), .Dim = 0:1)), .Names = ""),
structure(list(structure(character(0), .Dim = 0:1)), .Names = ""),
structure(list(structure(character(0), .Dim = 0:1)), .Names = ""),
structure(list(structure(character(0), .Dim = 0:1)), .Names = "")),
structure(c(50, 2, 2, 2, 1, 1), .Dim = c(1L, 6L)), structure(c(1,
0.4937744140625, 0.4903564453125, 0.4732666015625, 1, 1), .Dim = c(1L,
6L)), structure(c(0, 0, 0, 0, 0, 32768), .Dim = c(1L, 6L)),
list(structure(list(structure("ECG", .Dim = c(1L, 1L))), .Names = "")),
structure(c(0, 0, 0, 0, 0, 32768), .Dim = c(1L, 6L)), structure(c(205.8,
205.8, 205.8, 205.8, 205.8, 205.8), .Dim = c(6L, 1L))), .Dim = c(14L,
1L, 1L), .Dimnames = list(c("label", "TransType", "PhysDim",
"PhysMin", "PhysMax", "DigiMin", "DigiMax", "PreFilt", "SamplesInDataRec",
"calib", "Offset", "readchannel", "baseline", "sens"), NULL,
NULL)))