2

I have a MATLAB nested structure in a .mat file that I can read using readMat from the R package R.matlab.

The ouput of readMat is a list.

My question is whether there is any standard general approach that can be applied to convert this type of lists into expanded data.frames.

Example:

MATLAB code to create the nested structure with fields:

s(1).field1(1).subfield1 = rand(3,1)
s(1).field1(2).subfield1 = rand(3,1)
s(1).field1(1).subfield2 = rand(3,1)
s(1).field1(2).subfield2 = rand(3,1)

s(2).field1(1).subfield1 = rand(3,1)
s(2).field1(2).subfield1 = rand(3,1)
s(2).field1(1).subfield2 = rand(3,1)
s(2).field1(2).subfield2 = rand(3,1)

s(1).field2(1).subfield1 = rand(3,1)
s(1).field2(2).subfield1 = rand(3,1)
s(1).field2(1).subfield2 = rand(3,1)
s(1).field2(2).subfield2 = rand(3,1)

s(2).field2(1).subfield1 = rand(3,1)
s(2).field2(2).subfield1 = rand(3,1)
s(2).field2(1).subfield2 = rand(3,1)
s(2).field2(2).subfield2 = rand(3,1)

save('structarray.mat','s')

R code to read the structure:

library(R.matlab)
dat <- readMat('matlab/structarray.mat') # dat is a nested list

My aim is to obtain a data.frame like this:

field nfield  subfield nsubfield           y
field1      1 subfield1         1 0.380052099
field1      1 subfield1         1 0.035129304
field1      1 subfield1         1 0.008511324
field1      1 subfield1         2 0.649426077
field1      1 subfield1         2 0.826891592
field1      1 subfield1         2 0.430126008
field1      1 subfield2         1 0.815601068
field1      1 subfield2         1 0.572206327
field1      1 subfield2         1 0.746530501
field1      1 subfield2         2 0.859147935
field1      1 subfield2         2 0.837791589
field1      1 subfield2         2 0.399018405
field1      2 subfield1         1 0.643160134
field1      2 subfield1         1 0.481749578
field1      2 subfield1         1 0.217701439
field1      2 subfield1         2 0.566511370
field1      2 subfield1         2 0.103022946
field1      2 subfield1         2 0.252435623
field1      2 subfield2         1 0.336995935
field1      2 subfield2         1 0.621518281
field1      2 subfield2         1 0.831389850
field1      2 subfield2         2 0.875852150
field1      2 subfield2         2 0.496274841
field1      2 subfield2         2 0.244327029
field2      1 subfield1         1 0.160029320
field2      1 subfield1         1 0.486741324
field2      1 subfield1         1 0.928049809
field2      1 subfield1         2 0.664060046
field2      1 subfield1         2 0.494835926
field2      1 subfield1         2 0.334187631
field2      1 subfield2         1 0.848695033
field2      1 subfield2         1 0.678903437
field2      1 subfield2         1 0.727952232
field2      1 subfield2         2 0.736806528
field2      1 subfield2         2 0.033262655
field2      1 subfield2         2 0.146987385
field2      2 subfield1         1 0.479585294
field2      2 subfield1         1 0.415707860
field2      2 subfield1         1 0.284633795
field2      2 subfield1         2 0.373401112
field2      2 subfield1         2 0.894219236
field2      2 subfield1         2 0.915542871
field2      2 subfield2         1 0.091142321
field2      2 subfield2         1 0.849635273
field2      2 subfield2         1 0.887008165
field2      2 subfield2         2 0.174631935
field2      2 subfield2         2 0.512926207
field2      2 subfield2         2 0.707098742

Here the dput of dat:

dput(dat)

structure(list(s =
structure(list(structure(list(structure(c(0.656694003304474, 
0.290185566632867, 0.754536639716295), .Dim = c(3L, 1L)),  
structure(c(0.753736074133061, 
0.898376325005631, 0.728444033957786), .Dim = c(3L, 1L)),
structure(c(0.558118051358972, 
0.427792595548456, 0.2671940572742), .Dim = c(3L, 1L)), 
structure(c(0.406830134654488, 
0.938315840253697, 0.255427454937803), .Dim = c(3L, 1L))), .Dim = c(2L, 
1L, 2L), .Dimnames = list(c("subfield1", "subfield2"), NULL, 
NULL)), structure(list(structure(c(0.650812135949142, 0.795954985556127, 
0.233373768071567), .Dim = c(3L, 1L)), structure(c(0.837840690870234, 
0.920790107998705, 0.498227929455356), .Dim = c(3L, 1L)),  
structure(c(0.600838938434792, 
0.112462378383286, 0.515765653195914), .Dim = c(3L, 1L)), 
structure(c(0.277611122264388, 
0.652519960748294, 0.917298803800668), .Dim = c(3L, 1L))), .Dim = c(2L, 
1L, 2L), .Dimnames = list(c("subfield1", "subfield2"), NULL, 
NULL)), structure(list(structure(c(0.533163240608823, 0.95475487037637, 
0.267747536440096), .Dim = c(3L, 1L)), structure(c(0.299400386560865, 
0.591583534156367, 0.203299145356336), .Dim = c(3L, 1L)),
structure(c(0.250084620589589, 
0.927672720239348, 0.0685823485554556), .Dim = c(3L, 1L)),
structure(c(0.635883198886887, 
0.798370257830259, 0.501701052364218), .Dim = c(3L, 1L))), .Dim = c(2L, 
1L, 2L), .Dimnames = list(c("subfield1", "subfield2"), NULL, 
NULL)), structure(list(structure(c(0.509839453514735, 0.974191483802598, 
0.197278942097314), .Dim = c(3L, 1L)), structure(c(0.420755683348492, 
0.311475357650453, 0.693843170932226), .Dim = c(3L, 1L)),
structure(c(0.111184983788934, 
0.297354293368622, 0.396418535561727), .Dim = c(3L, 1L)), 
structure(c(0.0918718332714995, 
0.402088619037046, 0.295180804029962), .Dim = c(3L, 1L))), .Dim = c(2L, 
1L, 2L), .Dimnames = list(c("subfield1", "subfield2"), NULL, 
NULL))), .Dim = c(2L, 1L, 2L), .Dimnames = list(c("field1", 
"field2"), NULL, NULL))), .Names = "s", header = structure(list(
description = "MATLAB 5.0 MAT-file, Platform: MACI64, Created on: Thu Jul 21   
14:57:50 2016                                         ", 
version = "5", endian = "little"), .Names = c("description", 
"version", "endian")))
danilinares
  • 1,172
  • 1
  • 9
  • 28
  • does this help mydata <- lapply(structarray, unlist, use.names=FALSE); df <- as.data.frame(mydata) –  Jul 21 '16 at 13:40
  • you can also try this **do.call(rbind.data.frame, structarray)** –  Jul 21 '16 at 13:43
  • The "lapply" atttempt produces the last row of the intended dataframe (it loses the labeling provided by the other columns). The "do.call" does not work on my computer. – danilinares Jul 21 '16 at 13:48
  • 1
    can you get a dput from the list file you load in R? just post small portion of it , if it is big –  Jul 21 '16 at 13:57

0 Answers0