NMath Stats User's Guide

TOC | Previous | Next | Index

2.4 Adding and Removing Rows (.NET, C#, CSharp, VB, Visual Basic, F#)

The AddRow() method adds a row of data to a data frame. The first parameter is the row key; subsequent parameters are the row data. For example:

Code Example – C#

var df = new DataFrame();
df.AddColumn( new DFStringColumn( "Col1" ));  
df.AddColumn( new DFNumericColumn( "Col2" ) );
df.AddColumn( new DFNumericColumn( "Col3" ) );
df.AddRow( 1546, "Test1", 1.5445, 667.87 );

NOTE—The AddRow() method raises a MismatchedSizeException if the number of row elements does not match the number of columns in the data frame.

This example uses 1546 as an integer row key, perhaps representing some sort of ID. Row keys can be any object, and need not be unique.

Additional overloads of AddRow() accept data in various collections other than an array of objects. One overload takes an ICollection. For instance:

Code Example – C#

var myQ = new Queue();
myQ.Enqueue( "Hello" );
myQ.Enqueue( 47.0 );
myQ.Enqueue( -0.34 );
df.AddRow( "Row1", myQ );

Another overload accepts an IDictionary in which the keys are the column names and the values are the row data:

Code Example – C#

var df = new DataFrame();
df.AddColumn( new DFNumericColumn( "V1" ) );
df.AddColumn( new DFBoolColumn( "V2" ) );
df.AddColumn( new DFStringColumn( "V3" ) );
var myHT = new Hashtable();
myHT.Add( "V1", 3.14 );
myHT.Add( "V3", "Hello");
myHT.Add( "V2", true );
df.AddRow( "Row1", myHT );

If all of the columns in your data frame are numeric, you can add a row as a DoubleVector:

Code Example – C#

var v = new DoubleVector( 10, 0, 1 );
df.AddRow( "myKey", v );

Other overloads of AddRow() and AddRows() accept ADO.NET DataRow and DataRowCollection instances, respectively.

InsertRow() inserts a row at a given row index. For example, this code inserts a row into the second position:

Code Example – C#

var df = new DataFrame();
df.AddColumn( new DFNumericColumn( "Col1" ) );
df.AddColumn( new DFNumericColumn( "Col2" ) );
df.AddColumn( new DFNumericColumn( "Col3" ) );
df.AddRow( "Row1", 2.5, 0.0, 3.4 );
df.AddRow( "Row2", 3.14, -.5, -.33 );
df.AddRow( "Row3", 0.1, 55.34, 12.02 );
df.AddRow( "Row4", 3.14, -33.2, 7.22 );
var myRow = object[] { 5.5, 9.05, -6.11 };
df.InsertRow( 1, "Row1a", myRow );

Again, overloads are provided for adding row data in various collection types.

RemoveRow() removes the row at a given index:

Code Example – C#

df.RemoveRow( 0 );

You can also identify a row by key:

Code Example – C#

df.RemoveRow( "Row3" );

Because row keys are not constrained to be unique, this method will remove all rows in the data frame with the given key.

RemoveAllRows() removes all rows from a data frame, but preserves the existing columns. RemoveRows() removes the rows specified in a given subset or slice.

Clear() method removes all rows and columns from a data frame. CleanRows() returns a new data frame containing only those rows in a data frame that do not contain missing values.

Modifying Row Keys

Unlike column names which are fixed at construction time, row keys can be changed at any time. The SetRowKey() method sets the key for a given row to a given value. Remember that row keys can be any object:

Code Example – C#

df.SetRowKey( 0, 1.14 );
df.SetRowKey( 1, "John Doe" );
df.SetRowKey( 2, true );

SetRowKeys() accepts a collection of row keys, and raises a MismatchedSizeException if if the number of elements in the collection does not equal the number of rows in this data frame:

Code Example – C#

object[] keys = { "Subject1", "Subject2", "Subject3" };
df.SetRowKeys( keys );

Finally, IndexRowKeys() resets the row keys for all rows to rowIndex + 1; that is, 1...n.