Octave: converting dataframe to cell array

我与影子孤独终老i 提交于 2021-01-29 06:10:19

问题


Given an Octave dataframe object created as

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

pkg load dataframe
df = dataframe(c);

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

  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?

回答1:


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

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

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.

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.


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



来源:https://stackoverflow.com/questions/55391844/octave-converting-dataframe-to-cell-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!