如何将MATLAB结构加载到R数据框中? [英] How to load a MATLAB struct into a R data frame?

查看:89
本文介绍了如何将MATLAB结构加载到R数据框中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MATLAB结构,其中包含许多字段,这些字段一起描述了多个变量的100个观测值,如下所示(MATLAB输出):

I have a MATLAB struct, containing a number of fields which together describe, say, 100 observations of a number of variables, as follows (MATLAB output):

mystruct = 

  fieldA: [100x1 double]
  fieldB: [100x1 double]
  fieldC: [100x1 double]
  fieldD: [100x1 char]
  fieldE: {100x1 cell}

我想将R与这些数据,所以我将结构另存为.mat文件。并使用 R.matlab 包将其导入。因为我是R的新手,所以以下内容可能很笨拙,但我可以很好地访问各个字段(R代码):

I want to use R with this data, so I save the struct as a .mat file. and import it using the R.matlab package. Because I'm new to R, the following is likely clumsy, but I can access individual fields just fine (R code):

> f = readMat('myfile.mat')
> data = f$mystruct
> data
  , , 1

      [,1]         
  fieldA Numeric,100  
  fieldB Numeric,100  
  fieldC Numeric,100  
  fieldD Character,100
  fieldE List,100   

> data = data[, , 1]
> df <- data.frame(fieldA = data$fieldA, fieldB = data$fieldB)

OK ,所以这是一个问题:我如何概括以上内容,以便为原始结构中任意数量的字段生成一个数据帧?对于我的5字段示例,我可以手动执行此操作,但是下一个数据集有很多字段,并且我不想全部输入。

OK, so here is the question: how can I generalize the above so that a data frame is generated for an arbitrary number of fields in the original struct? For my 5-field example I can manually do it, but the next data set I have has many fields, and I don't want to enter them all.

每个这个问题,我尝试了 rbind() ldply(),它们构造了尺寸过大的数据帧(分别为1个变量的401 obs和105个变量的401 obs)。

As per this question, I tried rbind() and ldply(), which construct outrageously dimensioned data frames (401 obs of 1 variable and 401 obs of 105 variables respectively).

推荐答案

事实证明,MATLAB单元格数组( fieldE )被作为嵌套列表导入。使用 unlist 可以解决以下问题:

As it turns out, the MATLAB cell array (fieldE) was imported as a nested list. Using unlist takes care of the problem:

data = lapply(data, unlist, use.names=FALSE)
df <- as.data.frame(data) # now has correct number of obs and vars

感谢@koekenbakker对此的关键指责!

Thanks @koekenbakker for the critical pointer to this!

这篇关于如何将MATLAB结构加载到R数据框中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆