问题
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),
- Is it possible to access the underlying cell array?
- Is it there a conversion mechanism back to cell array?
- 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