Octave:将数据帧转换为单元格数组 [英] Octave: converting dataframe to cell array

查看:61
本文介绍了Octave:将数据帧转换为单元格数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个 Octave 数据帧对象创建为

Given an Octave dataframe object created as

c = cell(m,n);
%populate c...

pkg load dataframe
df = dataframe(c);

(参见 https://octave.sourceforge.io/dataframe/overview.html),

  1. 是否可以访问底层元胞数组?
  2. 是否有返回元胞数组的转换机制?
  3. 是否可以将 df 保存为 CSV?
  1. Is it possible to access the underlying cell array?
  2. Is it there a conversion mechanism back to cell array?
  3. Is it possible to save df to CSV?

推荐答案

是的.与任何对象一样,数据帧对象可以转换回 struct.

Yes. A dataframe object, like any object, can be converted back into a struct.

获得结果结构后,查找字段 x_name 以获取列名,并查找字段 x_data 以获取元胞数组形式的数据,即

Once you have the resulting struct, look for the fields x_name to get the column names, and x_data to get the data in the form of a cell array, i.e.

struct(df).x_data

至于转换为 csv,据我所知,dataframe 包似乎没有提供任何相关的方法(特别是该包没有提供重载的 @dataframe/csvwrite 方法).因此,我只需提取上述信息,然后从那里将其写入 csv 文件.

As for conversion to csv, the dataframe package does not seem to provide any relevant methods as far as I can tell (in particular the package does not provide an overloaded @dataframe/csvwrite method). Therefore, I'd just extract the information as above, and go about writing it into a csv file from there.

如果您不处理严格的数字数据,您可能想查看 io 中的 cell2csv/csv2cell 方法> 包(因为内置的 csvwrite 函数严格用于数字数据).
如果这不能完全满足您的要求,我可能只是通过自定义 fprintf 语句手动创建一个 csv 文件.

If you're not dealing with strictly numerical data, you might want to have a look at the cell2csv / csv2cell methods from the io package (since the built-in csvwrite function is strictly for numerical data).
And if that doesn't do exactly what you want, I'd probably just go for creating a csv file manually via custom fprintf statements.

附注.您通常可以通过pkg describe -verbose dataframe 查看包提供的方法,或者通过methods(dataframe)(甚至methods(df)).此外,如果您想访问重载方法的文档,例如说 summary 方法,那么这是这样做的语法:help @dataframe/summary

PS. You can generally see what methods a package provides via pkg describe -verbose dataframe, or the methods for a particular class via methods(dataframe) (or even methods(df)). Also, if you ever wanted to access the documentation for an overloaded method, e.g. say the summary method, then this is the syntax for doing so: help @dataframe/summary

这篇关于Octave:将数据帧转换为单元格数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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