Home
Products
Support
Blog
Resources
Company
NMath Stats User's Guide
TOC |  Previous |  Next |  Index

2.8 Accessing Sub-Frames

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:

Subset 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:

Subset 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:

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:

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

bool[] bArray = new bArray[ df.Rows ];
for ( int i = 0; i < df.Rows; i++ )
{
  bArray[i] = ( df[0][i] == "Test1" );
}
Subset 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:

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

TOC |  Previous |  Next |  Index

Copyright © 2008 CenterSpace Software, LLC. All rights reserved.
All trademarks and registered trademarks mentioned on this web site are the property of their respective owners.
Contact Webmaster