The contents of a data frame can be exported in various ways.
The ToDoubleMatrix() method exports all the numeric data in a data frame to a DoubleMatrix. Non-numeric columns are ignored. For example, this code constructs a DataFrame from a DoubleMatrix, adds a column of string data, then exports the contents of the data frame to another DoubleMatrix:
DoubleMatrix A = new DoubleMatrix( 8, 3, 0, .1 ); df = new DataFrame( A, new string[] { "A", "B", "C" } ); DFStringColumn col4 = new DFStringColumn( "D", new String[] { "x", "x", "x", "x", "x", "x", "x", "x" } ); df.AddColumn( col4 ); DoubleMatrix B = df.ToDoubleMatrix();
The two matrices are equal (A == B); the string column is ignored.
The ToString() method returns a formatted string representation of a data frame:
string str = df.ToString();
For more control, you can also indicate:
For instance, this code exports the column headers, but not the row keys, and uses a comma delimiter:
string str = df.ToString( true, false, "," );
Convenience methods are also provided for persisting a text representation of a data frame to a text file. Save() exports the contents of the data frame to a given filename:
df.Save( "myData.txt" );
Again, you can also indicate whether to export column header or row keys, and specify the column delimiter:
df.Save( "myData.txt", true, false, "," );
The LaunchSaveFileDialog() method allows the end user to specify the filename. The OpenInEditor() method programmatically opens a data frame in the default text editor on the user's system. The user can then edit the contents of the data frame. Lastly, the static Load() method imports a data frame from a text file:
DataFrame df = DataFrame.Load( "myData.txt" );
Again, you can indicate whether the text file includes column headers and row keys, and the delimiter used to separate the columns.
The ToDataTable() method exports the data in a data frame to an ADO.NET DataTable object. The row keys are placed in a DataColumn named DFRowKeys. Thus, this code:
DataFrame df = new DataFrame(); df.AddColumn( new DFNumericColumn( "ids", new DoubleVector( 3, 3, -1 ))); df.AddColumn( new DFStringColumn( "names", "a", "b", "c" )); df.AddColumn( new DFBoolColumn( "bools", true, false, true )); df.SetRowKeys( new String[] { "Row1", "Row2", "Row3" } ); DataTable table = df.ToDataTable();
returns a DataTable that looks like this:
name: CenterSpace.NMath.Stats.DataFrame # DFRowKeys ids names bools 1 Row1 3.0000 a True 2 Row2 2.0000 b False 3 Row3 1.0000 c True
If no name is assigned to a data frame before ToDataTable() is called, the name of the DataTable is set to the type: CenterSpace.NMath.Stats.DataFrame.
Class DataFrame implements the ISerializable interface to control serialization and deserialization. Common Language Runtime (CLR) serialization Formatter classes call the provided GetObjectData() method at serialization time to populate a SerializationInfo object with all the data required to represent a DataFrame. For example, the BinaryFormatter class provides Serialize() and Deserialize() methods for persisting an object in binary format to a given stream. For example, this code serializes a data frame to a file:
using System.IO; using System.Runtime.Serialization.Formatters.Binary; FileStream binStream = File.Create( "myData.dat" ); BinaryFormatter binFormatter = new BinaryFormatter(); binFormatter.Serialize( binStream, df ); binStream.Close();
This code restores the data frame from the file:
binStream = File.OpenRead( "myData.dat" ); DataFrame df2 = (DataFrame)binFormatter.Deserialize( binStream ); binStream.Close(); File.Delete( "myData.dat" );
Similarly, the SoapFormatter class persists an object in SOAP format to a given stream. Thus:
using System.IO; using System.Runtime.Serialization.Formatters.Soap; FileStream xmlStream = File.Create( "myData.xml" ); SoapFormatter xmlFormatter = new SoapFormatter(); xmlFormatter.Serialize( xmlStream, df ); xmlStream.Close();
This code restores the data frame from the file:
xmlStream = File.OpenRead( "myData.xml" ); DataFrame df2 = (DataFrame)xmlFormatter.Deserialize( xmlStream ) xmlStream.Close(); File.Delete( "myData.xml" );
TOC | Previous | Next | Index