Class Relation

java.lang.Object
tech.tablesaw.table.Relation
All Implemented Interfaces:
Iterable<Row>
Direct Known Subclasses:
Table, TableSlice

public abstract class Relation extends Object implements Iterable<Row>
A tabular data structure like a table in a relational database, but not formally implementing the relational algebra
  • Constructor Details

    • Relation

      public Relation()
  • Method Details

    • addColumns

      public abstract Relation addColumns(Column<?>... cols)
      Adds the given columns to this Relation and returns the same relation.
      Returns:
      This Relation
    • setName

      public abstract Relation setName(String name)
      Sets the name of this Relation and returns the same relation.
      Returns:
      This Relation
    • isEmpty

      public boolean isEmpty()
      Returns true if this relation has zero rows and false otherwise. Rows of missing values are counted.
    • shape

      public String shape()
      Returns a string that tells how many rows and columns this relation has
    • removeColumns

      public Relation removeColumns(int... columnIndexes)
      Removes the columns at the given 0-based indices from this Relation and returns the same relation.
      Returns:
      This Relation
    • removeColumns

      public abstract Relation removeColumns(Column<?>... columns)
      Removes the given columns from this Relation and returns the same relation.
      Returns:
      This Relation
    • removeColumns

      public Relation removeColumns(String... columnName)
      Removes the columns with the given namesfrom this Relation and returns the same relation.
      Returns:
      This Relation
    • columnsOfType

      public List<Column<?>> columnsOfType(ColumnType type)
      Returns a list containing all the columns of the given type in this Relation
    • first

      public abstract Table first(int nRows)
      Returns a new table containing the first n rows in this Relation
    • columnIndex

      public int columnIndex(String columnName)
      Returns the index of the column with the given columnName
    • column

      public Column<?> column(String columnName)
      Returns the column with the given columnName, ignoring case
    • column

      public abstract Column<?> column(int columnIndex)
      Returns the column at columnIndex (0-based)
      Parameters:
      columnIndex - an integer at least 0 and less than number of columns in the relation
      Returns:
      the column at the given index
    • columnCount

      public abstract int columnCount()
      Returns the number of columns in the relation
    • rowCount

      public abstract int rowCount()
      Returns the number of rows in the relation
    • columns

      public abstract List<Column<?>> columns()
      Returns a list of all the columns in the relation
    • columns

      public List<Column<?>> columns(String... columnName)
      Returns the columns whose names are given in the input array
    • columns

      public List<Column<?>> columns(int... columnIndices)
      Returns the columns whose indices are given in the input array
    • columnIndex

      public abstract int columnIndex(Column<?> col)
      Returns the index of the given column
    • get

      public Object get(int r, int c)
      Returns the value at the given row and column indexes
      Parameters:
      r - the row index, 0 based
      c - the column index, 0 based
    • name

      public abstract String name()
      Returns the name of this relation
    • clear

      public abstract void clear()
      Clears all the dat in the relation, leaving the structure intact
    • columnNames

      public abstract List<String> columnNames()
      Returns a list containing the names of all the columns in this relation
    • columnTypes

      @Deprecated public ColumnType[] columnTypes()
      Deprecated.
      for API name consistency. Use typeArray() instead.
      Returns an array of the column types of all columns in the relation, including duplicates as appropriate, and maintaining order
    • typeArray

      public ColumnType[] typeArray()
      Returns an array of the column types of all columns in the relation, including duplicates as appropriate, and maintaining order
    • types

      public List<ColumnType> types()
      Returns a List of the column types of all columns in the relation, including duplicates as appropriate, and maintaining order
    • colWidths

      public int[] colWidths()
      Returns an array of column widths for printing tables
    • toString

      public String toString()
      Returns a String containing a 'pretty-printed' representation of this table containing at most 20 rows. The 20 rows are the first and last ten in this table.
      Overrides:
      toString in class Object
    • printAll

      public String printAll()
      Returns a 'pretty-printed' string representation of this entire relation.
    • print

      public String print(int rowLimit)
      Returns a 'pretty-printed' string representation of at most rowLimit rows from this relation.
    • print

      public String print()
      Returns a String containing a 'pretty-printed' representation of this table containing at most 20 rows. The 20 rows are the first and last ten in this table.
    • structure

      public Table structure()
      Returns the structure of the this relation as a 3-column Table, consisting of Index (an IntColumn), Column Name (a StringColumn), and Column Type (a StringColumn)
    • summary

      public Table summary()
      Returns a table containing summary statistics for the columns in this Relation
    • booleanColumn

      public BooleanColumn booleanColumn(int columnIndex)
      Returns the DoubleColumn at the given 0-based index if present. A ClassCastException is the column is of a different type.
    • booleanColumn

      public BooleanColumn booleanColumn(String columnName)
      Returns a BooleanColumn with the given name if it is present in this Relation. If the column is of a different type, a ClassCastException is thrown
    • numberColumn

      public NumericColumn<?> numberColumn(int columnIndex)
      Returns the NumberColumn at the given index. If the index points to a String or a boolean column, a new NumberColumn is created and returned TODO(lwhite):Consider separating the indexed access and the column type mods, which must be for ML functions (in smile or elsewhere)
      Parameters:
      columnIndex - The 0-based index of a column in the table
      Returns:
      A number column
      Throws:
      ClassCastException - if the cast to NumberColumn fails
    • numberColumn

      public NumericColumn<?> numberColumn(String columnName)
      Returns a NumericColumn with the given name if it is present in this Relation. If the column is not Numeric, a ClassCastException is thrown
    • doubleColumn

      public DoubleColumn doubleColumn(String columnName)
      Returns a DoubleColumn with the given name if it is present in this Relation. If the column is not of type DOUBLE, a ClassCastException is thrown
    • doubleColumn

      public DoubleColumn doubleColumn(int columnIndex)
      Returns the DoubleColumn at the given 0-based index if present. A ClassCastException is the column is of a different type.
    • stringColumns

      public StringColumn[] stringColumns()
      Returns all the StringColumns in this Relation as an Array
    • numberColumns

      public NumericColumn<?>[] numberColumns()
      Returns all the NumericColumns in this Relation as an Array
    • numericColumns

      public List<NumericColumn<?>> numericColumns()
      Returns all the NumericColumns in the relation
    • numericColumns

      public List<NumericColumn<?>> numericColumns(int... columnIndices)
      Returns all the NumericColumns in the relation
    • numericColumns

      public List<NumericColumn<?>> numericColumns(String... columnNames)
      Returns all the NumericColumns in the relation
    • booleanColumns

      public BooleanColumn[] booleanColumns()
      Returns all BooleanColumns in this Relation as an Array
    • dateColumns

      public DateColumn[] dateColumns()
      Returns all DateColumns in this Relation as an Array
    • dateTimeColumns

      public DateTimeColumn[] dateTimeColumns()
      Returns all DateTimeColumns in this Relation as an Array
    • instantColumns

      public InstantColumn[] instantColumns()
      Returns all InstantColumns in this Relation as an Array
    • timeColumns

      public TimeColumn[] timeColumns()
      Returns all TimeColumns in this Relation as an Array
    • categoricalColumn

      public CategoricalColumn<?> categoricalColumn(String columnName)
      Returns a CategoricalColumn with the given name if it is present in this Relation. If the column is not Categorical, a ClassCastException is thrown
    • categoricalColumn

      public CategoricalColumn<?> categoricalColumn(int columnNumber)
      Returns the CategoricalColumn at the given 0-based index if present. A ClassCastException is thrown otherwise
    • categoricalColumns

      public List<CategoricalColumn<?>> categoricalColumns(String... columnName)
      Returns the columns whose names are given in the input array
    • nCol

      public NumericColumn<?> nCol(String columnName)
      Returns the column with the given name cast to a NumberColumn. If the column is not Numeric, a ClassCastException is thrown

      Shorthand for numberColumn()

    • nCol

      public NumericColumn<?> nCol(int columnIndex)
      Returns the column with the given name cast to a NumberColumn

      Shorthand for numberColumn()

    • intColumn

      public IntColumn intColumn(String columnName)
      Returns an IntColumn with the given name if it is present in this Relation. If the column has a different type, a ClassCastException is thrown.
    • intColumn

      public IntColumn intColumn(int columnIndex)
      Returns the IntColumn at the given 0-based index if present. A ClassCastException is the column is of a different type.
    • shortColumn

      public ShortColumn shortColumn(String columnName)
      Returns a ShortColumn with the given name if it is present in this Relation. If the column has a different type, a ClassCastException is thrown.
    • shortColumn

      public ShortColumn shortColumn(int columnIndex)
      Returns the ShortColumn at the given 0-based index if present. A ClassCastException is the column is of a different type.
    • longColumn

      public LongColumn longColumn(String columnName)
      Returns a LongColumn with the given name if it is present in this Relation. If the column has a different type, a ClassCastException is thrown.
    • longColumn

      public LongColumn longColumn(int columnIndex)
      Returns the LongColumn at the given 0-based index if present. A ClassCastException is the column is of a different type.
    • floatColumn

      public FloatColumn floatColumn(String columnName)
      Returns a FloatColumn with the given name if it is present in this Relation. If the column has a different type, a ClassCastException is thrown.
    • floatColumn

      public FloatColumn floatColumn(int columnIndex)
      Returns the FloatColumn at the given 0-based index if present. A ClassCastException is the column is of a different type.
    • dateColumn

      public DateColumn dateColumn(int columnIndex)
      Returns the DateColumn at the given 0-based index if present. A ClassCastException is the column is of a different type.
    • dateColumn

      public DateColumn dateColumn(String columnName)
      Returns a DateColumn with the given name if it is present in this Relation. If the column has a different type, a ClassCastException is thrown.
    • timeColumn

      public TimeColumn timeColumn(String columnName)
      Returns a TimeColumn with the given name if it is present in this Relation. If the column has a different type, a ClassCastException is thrown.
    • timeColumn

      public TimeColumn timeColumn(int columnIndex)
      Returns the TimeColumn at the given 0-based index if present. A ClassCastException is the column is of a different type.
    • stringColumn

      public StringColumn stringColumn(String columnName)
      Returns a StringColumn with the given name if it is present in this Relation. If the column has a different type, a ClassCastException is thrown.
    • stringColumn

      public StringColumn stringColumn(int columnIndex)
      Returns the StringColumn at the given 0-based index if present. A ClassCastException is the column is of a different type.
    • textColumn

      public TextColumn textColumn(String columnName)
      Returns a TextColumn with the given name if it is present in this Relation. If the column has a different type, a ClassCastException is thrown.
    • textColumn

      public TextColumn textColumn(int columnIndex)
      Returns the TextColumn at the given 0-based index if present. A ClassCastException is the column is of a different type.
    • dateTimeColumn

      public DateTimeColumn dateTimeColumn(int columnIndex)
      Returns the DateTimeColumn at the given 0-based index if present. A ClassCastException is the column is of a different type.
    • dateTimeColumn

      public DateTimeColumn dateTimeColumn(String columnName)
      Returns a DateTimeColumn with the given name if it is present in this Relation. If the column has a different type, a ClassCastException is thrown.
    • instantColumn

      public InstantColumn instantColumn(int columnIndex)
      Returns the InstantColumn at the given 0-based index if present. A ClassCastException is the column is of a different type.
    • instantColumn

      public InstantColumn instantColumn(String columnName)
      Returns an InstantColumn with the given name if it is present in this Relation. If the column has a different type, a ClassCastException is thrown.
    • as

      public TableConverter as()
      Returns an TableConverter that can convert this Relation to a two-dimensional matrix of primitive numeric values
    • smile

      public SmileConverter smile()
      Returns an SmileConverter that can convert this table to a format suitable for use with the Smile machine learning library.
    • getUnformatted

      public String getUnformatted(int r, int c)
      Returns a string representation of the value at the given row and column indexes
      Parameters:
      r - the row index, 0 based
      c - the column index, 0 based
    • getString

      public String getString(int r, String columnName)
      Returns a string representation of the value at the given row and column indexes
      Parameters:
      r - the row index, 0 based
      columnName - the name of the column to be returned

      // TODO: performance would be enhanced if columns could be referenced via a hashTable

    • getString

      public String getString(int r, int columnIndex)
      Returns a string representation of the value at the given row and column indexes
      Parameters:
      r - the row index, 0 based
      columnIndex - the index of the column to be returned

      // TODO: performance would be enhanced if columns could be referenced via a hashTable

    • containsColumn

      public boolean containsColumn(Column<?> column)
      Returns true if the given column is in this Relation
    • containsColumn

      public boolean containsColumn(String columnName)
      Returns true if a column with the given name is in this Relation