NMath Stats User's Guide

TOC | Previous | Next | Index

2.8 Accessing Sub-Frames (.NET, C#, CSharp, VB, Visual Basic, F#)

In addition to accessing individual elements, columns, or rows in a data frame (Section 2.6), class DataFrame provides a large number of member functions and indexers for accessing sub-frames containing any arbitrary subset of rows, columns, or both. Such methods and indexers accept Slice and Subset objects to indicate which rows and columns to return. (See Section 2.7 for more information on the Subset class.)

For example, GetColumns() returns a new data frame containing the columns indicated by a given Slice or Subset. For instance, if df has 5 columns, this code creates a new data frame containing columns 0, 4, and 5:

Code Example – C#

var colSubset = new Subset( new int[] { 0, 4, 5 } );
DataFrame subDF = df.GetColumns( colSubset );

Similarly, GetRows() returns a new data frame containing the rows indicated by a given Slice or Subset. Thus, this code gets every other row in the source data frame:

Code Example – C#

var rowSubset = new Range( 0, df.Rows - 1, 2 );
DataFrame subDF = df.GetRows( rowSubset );

Class DataFrame also provides a wide range of indexers for accessing subframes:

Code Example – C#

this[int colIndex, Slice rowSlice]
this[int colIndex, Subset rowSubset]
this[Slice rowSlice, Slice colSlice]
this[Subset rowSubset, Subset colSubset]
this[Slice rowSlice, Subset colSubset]
this[Subset rowSubset, Slice colSlice]

These indexers can be used to return any portion of a data frame. For example, this code gets a new data frame containing columns 3-8 in reverse order, and all rows where column 0 equals Test1:

Code Example – C#

var colRange = new Range( 8, 3, -1 );

var bArray = new bool[ df.Rows ];
for ( int i = 0; i < df.Rows; i++ )
{
  bArray[i] = ( df[0][i] == "Test1" );
}
var rowSubset = new Subset( bArray );

DataFrame df2 = df[ rowSubset, colRange ];

Finally, there is the GetSubRow() method. Whereas GetRow() returns an entire row for a given row index, GetSubRow() returns the portion of the row indicated by the given column Slice or Subset:

Code Example – C#

var colSlice = new Slice( 0, 3, 1 );
object[] subRow = df.GetSubRow( 3, colSlice );

Top

Top