Package processing.data
Class Table
java.lang.Object
processing.data.Table
Table objects store data with multiple rows and columns, much like in
a traditional spreadsheet. Tables can be generated from scratch, dynamically,
or using data from an existing file. Tables can also be output and saved to
disk, as in the example above.
Additional Table methods are documented in the Processing Table Javadoc.
Additional Table methods are documented in the Processing Table Javadoc.
Advanced
Generic class for handling tabular data, typically from a CSV, TSV, or other sort of spreadsheet file.
CSV files are comma separated values, often with the data in quotes. TSV files use tabs as separators, and usually don't bother with the quotes.
File names should end with .csv if they're comma separated.
A rough "spec" for CSV can be found here.
-
Field Summary
-
Constructor Summary
ConstructorDescriptionTable()
Creates a new, empty table.version that uses a File object; future releases (or data types) may include additional optimizations hereTable
(InputStream input) Table
(InputStream input, String options) Read the table from a stream. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Use addColumn() to add a new column to a Table object.void
void
addRow()
Use addRow() to add a new row of data to a Table object.void
int
checkColumnIndex
(String title) Same as getColumnIndex(), but creates the column if it doesn't exist.void
Removes all rows from a Table.copy()
Make a copy of the current tablestatic String
extensionOptions
(boolean loading, String filename, String options) Finds the first row in the Table that contains the value provided, and returns a reference to that row.int
findRowIndex
(String value, int column) Return the row that contains the first String that matches.int
findRowIndex
(String value, String columnName) Return the row that contains the first String that matches.int[]
findRowIndices
(String value, int column) Return a list of rows that contain the String passed in.int[]
findRowIndices
(String value, String columnName) Return a list of rows that contain the String passed in.findRowIterator
(String value, int column) findRowIterator
(String value, String columnName) Finds the rows in the Table that contain the value provided, and returns references to those rows.int
Returns the total number of columns in a table.int
getColumnIndex
(String columnName) getColumnTitle
(int col) String[]
int
getColumnType
(int column) Returns one of Table.STRING, Table.INT, etc...int
getColumnType
(String columnName) int[]
double
getDouble
(int row, int column) double
double[]
getDoubleColumn
(int col) double[]
getDoubleColumn
(String name) double[]
getDoubleRow
(int row) float
getFloat
(int row, int column) Retrieves a float value from the Table's specified row and column.float
float[]
getFloatColumn
(int col) float[]
getFloatColumn
(String name) getFloatDict
(int keyColumn, int valueColumn) getFloatDict
(String keyColumnName, String valueColumnName) getFloatList
(int column) getFloatList
(String columnName) float[]
getFloatRow
(int row) int
getInt
(int row, int column) Retrieves an integer value from the Table's specified row and column.int
int[]
getIntColumn
(int col) int[]
getIntColumn
(String name) getIntDict
(int keyColumn, int valueColumn) getIntDict
(String keyColumnName, String valueColumnName) getIntList
(int column) getIntList
(String columnName) int[]
getIntRow
(int row) long
getLong
(int row, int column) long
long[]
getLongColumn
(int col) long[]
getLongColumn
(String name) long[]
getLongRow
(int row) getOrder
(int column) getRow
(int row) Returns a reference to the specified TableRow.int
Returns the total number of rows in a Table.getRowMap
(int column) Return a mapping that connects the entry from a column back to the row from which it came.getString
(int row, int column) Retrieves a String value from the Table's specified row and column.String[]
getStringColumn
(int column) String[]
getStringColumn
(String columnName) Retrieves all values in the specified column, and returns them as a String array.getStringDict
(int keyColumn, int valueColumn) getStringDict
(String keyColumnName, String valueColumnName) getStringList
(int column) getStringList
(String columnName) String[]
getStringRow
(int row) getTally
(int column) String[]
getUnique
(int column) String[]
boolean
void
insertColumn
(int index) void
insertColumn
(int index, String title) void
insertColumn
(int index, String title, int type) void
int
Finds the first row in the Table that matches the regular expression provided, and returns a reference to that row.int
matchRowIndex
(String regexp, int column) Return the row that contains the first String that matches.int
matchRowIndex
(String what, String columnName) Return the row that contains the first String that matches.int[]
matchRowIndices
(String regexp, int column) Return a list of rows that contain the String passed in.int[]
matchRowIndices
(String what, String columnName) Return a list of rows that match the regex passed in.matchRowIterator
(String value, int column) Finds multiple rows that match the given expressionmatchRowIterator
(String value, String columnName) Finds the rows in the Table that match the regular expression provided, and returns references to those rows.void
incomplete, do not usevoid
print()
void
removeColumn
(int column) void
removeColumn
(String columnName) Use removeColumn() to remove an existing column from a Table object.void
removeRow
(int row) Removes a row from a Table objectString[]
Deprecated.void
removeTokens
(String tokens) Removes any of the specified characters (or "tokens").void
removeTokens
(String tokens, int column) Removed any of the specified characters from a column.void
removeTokens
(String tokens, String columnName) void
Replace a String with another.void
void
void
replaceAll
(String regex, String replacement) void
replaceAll
(String regex, String replacement, int column) void
replaceAll
(String regex, String replacement, String columnName) Run String.replaceAll() on all entries in a column.rows()
Gets all rows from the table.rows
(int[] indices) boolean
boolean
save
(OutputStream output, String options) void
setColumnCount
(int newCount) Change the number of columns in this table.void
setColumnTitle
(int column, String title) void
setColumnTitles
(String[] titles) void
setColumnType
(int column, int newType) Sets the column type.void
setColumnType
(int column, String columnType) Set the data type for a column so that using it is more efficient.void
setColumnType
(String columnName, int newType) void
setColumnType
(String columnName, String columnType) void
setColumnTypes
(int[] types) void
setColumnTypes
(Table dictionary) Set the titles (and if a second column is present) the data types for this table based on a file loaded separately.void
setDouble
(int row, int column, double value) void
void
setFloat
(int row, int column, float value) Stores a float value in the Table's specified row and column.void
void
setInt
(int row, int column, int value) Stores an integer value in the Table's specified row and column.void
void
setLong
(int row, int column, long value) void
void
setMissingDouble
(double value) void
setMissingFloat
(float value) void
setMissingInt
(int value) void
setMissingLong
(long value) void
setMissingString
(String value) Treat entries with this string as "missing".void
void
setRowCount
(int newCount) void
Stores a String value in the Table's specified row and column.void
void
setTableType
(String type) Set the entire table to a specific data type.void
sort
(int column) void
Sorts (orders) a table based on the values in a column.void
sortReverse
(int column) void
sortReverse
(String columnName) void
trim()
Trims leading and trailing whitespace, such as spaces and tabs, from String table values.void
trim
(int column) void
typedParse
(InputStream input, String options) void
write
(PrintWriter writer)
-
Field Details
-
STRING
public static final int STRING- See Also:
-
INT
public static final int INT- See Also:
-
LONG
public static final int LONG- See Also:
-
FLOAT
public static final int FLOAT- See Also:
-
DOUBLE
public static final int DOUBLE- See Also:
-
CATEGORY
public static final int CATEGORY- See Also:
-
-
Constructor Details
-
Table
public Table()Creates a new, empty table. Use addRow() to add additional rows. -
Table
- Throws:
IOException
-
Table
version that uses a File object; future releases (or data types) may include additional optimizations here- Throws:
IOException
-
Table
- Throws:
IOException
-
Table
Read the table from a stream. Possible options include:- csv - parse the table as comma-separated values
- tsv - parse the table as tab-separated values
- newlines - this CSV file contains newlines inside individual cells
- header - this table has a header (title) row
- Parameters:
input
-options
-- Throws:
IOException
-
Table
-
Table
-
-
Method Details
-
typedParse
- Throws:
IOException
-
extensionOptions
-
parseInto
incomplete, do not use -
save
- Throws:
IOException
-
save
-
addColumn
public void addColumn()Use addColumn() to add a new column to a Table object. Typically, you will want to specify a title, so the column may be easily referenced later by name. (If no title is specified, the new column's title will be null.) A column type may also be specified, in which case all values stored in this column must be of the same type (e.g., Table.INT or Table.FLOAT). If no type is specified, the default type of STRING is used.- See Also:
-
addColumn
- Parameters:
title
- the title to be used for the new column
-
addColumn
- Parameters:
type
- the type to be used for the new column: INT, LONG, FLOAT, DOUBLE, or STRING
-
insertColumn
public void insertColumn(int index) -
insertColumn
-
insertColumn
-
removeColumn
Use removeColumn() to remove an existing column from a Table object. The column to be removed may be identified by either its title (a String) or its index value (an int). removeColumn(0) would remove the first column, removeColumn(1) would remove the second column, and so on.- Parameters:
columnName
- the title of the column to be removed- See Also:
-
removeColumn
public void removeColumn(int column) - Parameters:
column
- the index number of the column to be removed
-
getColumnCount
public int getColumnCount()Returns the total number of columns in a table.- See Also:
-
setColumnCount
public void setColumnCount(int newCount) Change the number of columns in this table. Resizes all rows to ensure the same number of columns in each row. Entries in the additional (empty) columns will be set to null.- Parameters:
newCount
-
-
setColumnType
-
setColumnType
Set the data type for a column so that using it is more efficient.- Parameters:
column
- the column to changecolumnType
- One of int, long, float, double, string, or category.
-
setColumnType
-
setColumnType
public void setColumnType(int column, int newType) Sets the column type. If data already exists, then it'll be converted to the new type.- Parameters:
column
- the column whose type should be changednewType
- something fresh, maybe try an int or a float for size?
-
setTableType
Set the entire table to a specific data type. -
setColumnTypes
public void setColumnTypes(int[] types) -
setColumnTypes
Set the titles (and if a second column is present) the data types for this table based on a file loaded separately. This will look for the title in column 0, and the type in column 1. Better yet, specify a column named "title" and another named "type" in the dictionary table to future-proof the code.- Parameters:
dictionary
-
-
getColumnType
-
getColumnType
public int getColumnType(int column) Returns one of Table.STRING, Table.INT, etc... -
getColumnTypes
public int[] getColumnTypes() -
removeTitleRow
Deprecated.Remove the first row from the data set, and use it as the column titles. Use loadTable("table.csv", "header") instead. -
setColumnTitles
-
setColumnTitle
-
hasColumnTitles
public boolean hasColumnTitles() -
getColumnTitles
-
getColumnTitle
-
getColumnIndex
-
checkColumnIndex
Same as getColumnIndex(), but creates the column if it doesn't exist. Named this way to not conflict with checkColumn(), an internal function used to ensure that a columns exists, and also to denote that it returns an int for the column index.- Parameters:
title
- column title- Returns:
- index of a new or previously existing column
-
getRowCount
public int getRowCount()Returns the total number of rows in a Table.- See Also:
-
lastRowIndex
public int lastRowIndex() -
clearRows
public void clearRows()Removes all rows from a Table. While all rows are removed, columns and column titles are maintained.- See Also:
-
setRowCount
public void setRowCount(int newCount) -
addRow
Use addRow() to add a new row of data to a Table object. By default, an empty row is created. Typically, you would store a reference to the new row in a TableRow object (see newRow in the example above), and then set individual values using setInt(), setFloat(), or setString(). If a TableRow object is included as a parameter, then that row is duplicated and added to the table.- See Also:
-
addRow
- Parameters:
source
- a reference to the original row to be duplicated
-
setRow
-
addRow
-
addRows
-
insertRow
-
removeRow
public void removeRow(int row) Removes a row from a Table object- Parameters:
row
- ID number of the row to remove- See Also:
-
setRow
-
getRow
Returns a reference to the specified TableRow. The reference can then be used to get and set values of the selected row, as illustrated in the example above.- Parameters:
row
- ID number of the row to get- See Also:
-
rows
Gets all rows from the table. Returns an iterator, so for must be used to iterate through all the rows, as shown in the example above. -
rows
-
getInt
public int getInt(int row, int column) Retrieves an integer value from the Table's specified row and column. The row is specified by its ID, while the column may be specified by either its ID or title.- Parameters:
row
- ID number of the row to referencecolumn
- ID number of the column to reference- See Also:
-
getInt
- Parameters:
columnName
- title of the column to reference
-
setMissingInt
public void setMissingInt(int value) -
setInt
public void setInt(int row, int column, int value) Stores an integer value in the Table's specified row and column. The row is specified by its ID, while the column may be specified by either its ID or title.- Parameters:
row
- ID number of the target rowcolumn
- ID number of the target columnvalue
- value to assign- See Also:
-
setInt
- Parameters:
columnName
- title of the target column
-
getIntColumn
-
getIntColumn
public int[] getIntColumn(int col) -
getIntRow
public int[] getIntRow(int row) -
getLong
public long getLong(int row, int column) -
getLong
-
setMissingLong
public void setMissingLong(long value) -
setLong
public void setLong(int row, int column, long value) -
setLong
-
getLongColumn
-
getLongColumn
public long[] getLongColumn(int col) -
getLongRow
public long[] getLongRow(int row) -
getFloat
public float getFloat(int row, int column) Retrieves a float value from the Table's specified row and column. The row is specified by its ID, while the column may be specified by either its ID or title.- Parameters:
row
- ID number of the row to referencecolumn
- ID number of the column to reference- See Also:
-
getFloat
- Parameters:
columnName
- title of the column to reference
-
setMissingFloat
public void setMissingFloat(float value) -
setFloat
public void setFloat(int row, int column, float value) Stores a float value in the Table's specified row and column. The row is specified by its ID, while the column may be specified by either its ID or title.- Parameters:
row
- ID number of the target rowcolumn
- ID number of the target columnvalue
- value to assign- See Also:
-
setFloat
- Parameters:
columnName
- title of the target column
-
getFloatColumn
-
getFloatColumn
public float[] getFloatColumn(int col) -
getFloatRow
public float[] getFloatRow(int row) -
getDouble
public double getDouble(int row, int column) -
getDouble
-
setMissingDouble
public void setMissingDouble(double value) -
setDouble
public void setDouble(int row, int column, double value) -
setDouble
-
getDoubleColumn
-
getDoubleColumn
public double[] getDoubleColumn(int col) -
getDoubleRow
public double[] getDoubleRow(int row) -
getString
Retrieves a String value from the Table's specified row and column. The row is specified by its ID, while the column may be specified by either its ID or title.- Parameters:
row
- ID number of the row to referencecolumn
- ID number of the column to reference- See Also:
-
getString
- Parameters:
columnName
- title of the column to reference
-
setMissingString
Treat entries with this string as "missing". Also used for categorial. -
setString
Stores a String value in the Table's specified row and column. The row is specified by its ID, while the column may be specified by either its ID or title.- Parameters:
row
- ID number of the target rowcolumn
- ID number of the target columnvalue
- value to assign- See Also:
-
setString
- Parameters:
columnName
- title of the target column
-
getStringColumn
Retrieves all values in the specified column, and returns them as a String array. The column may be specified by either its ID or title.- Parameters:
columnName
- title of the column to search- See Also:
-
getStringColumn
- Parameters:
column
- ID number of the column to search
-
getStringRow
-
findRowIndex
Return the row that contains the first String that matches.- Parameters:
value
- the String to matchcolumn
- ID number of the column to search
-
findRowIndex
Return the row that contains the first String that matches.- Parameters:
value
- the String to matchcolumnName
- title of the column to search
-
findRowIndices
Return a list of rows that contain the String passed in. If there are no matches, a zero length array will be returned (not a null array).- Parameters:
value
- the String to matchcolumn
- ID number of the column to search
-
findRowIndices
Return a list of rows that contain the String passed in. If there are no matches, a zero length array will be returned (not a null array).- Parameters:
value
- the String to matchcolumnName
- title of the column to search
-
findRow
Finds the first row in the Table that contains the value provided, and returns a reference to that row. Even if multiple rows are possible matches, only the first matching row is returned. The column to search may be specified by either its ID or title.- Parameters:
value
- the value to matchcolumn
- ID number of the column to search- See Also:
-
findRow
- Parameters:
columnName
- title of the column to search
-
findRows
Finds the rows in the Table that contain the value provided, and returns references to those rows. Returns an iterator, so for must be used to iterate through all the rows, as shown in the example above. The column to search may be specified by either its ID or title.- Parameters:
value
- the value to matchcolumn
- ID number of the column to search- See Also:
-
findRows
- Parameters:
columnName
- title of the column to search
-
findRowIterator
- Parameters:
value
- the value to matchcolumn
- ID number of the column to search
-
findRowIterator
- Parameters:
columnName
- title of the column to search
-
matchRowIndex
Return the row that contains the first String that matches.- Parameters:
regexp
- the String to matchcolumn
- ID number of the column to search
-
matchRowIndex
Return the row that contains the first String that matches.- Parameters:
what
- the String to matchcolumnName
- title of the column to search
-
matchRowIndices
Return a list of rows that contain the String passed in. If there are no matches, a zero length array will be returned (not a null array).- Parameters:
regexp
- the String to matchcolumn
- ID number of the column to search
-
matchRowIndices
Return a list of rows that match the regex passed in. If there are no matches, a zero length array will be returned (not a null array).- Parameters:
what
- the String to matchcolumnName
- title of the column to search
-
matchRow
Finds the first row in the Table that matches the regular expression provided, and returns a reference to that row. Even if multiple rows are possible matches, only the first matching row is returned. The column to search may be specified by either its ID or title.- Parameters:
regexp
- the regular expression to matchcolumn
- ID number of the column to search- See Also:
-
matchRow
- Parameters:
columnName
- title of the column to search
-
matchRows
Finds the rows in the Table that match the regular expression provided, and returns references to those rows. Returns an iterator, so for must be used to iterate through all the rows, as shown in the example above. The column to search may be specified by either its ID or title.- Parameters:
regexp
- the regular expression to matchcolumn
- ID number of the column to search- See Also:
-
matchRows
- Parameters:
columnName
- title of the column to search
-
matchRowIterator
Finds multiple rows that match the given expression- Parameters:
value
- the regular expression to matchcolumn
- ID number of the column to search
-
matchRowIterator
- Parameters:
columnName
- title of the column to search
-
replace
Replace a String with another. Set empty entries null by using replace("", null) or use replace(null, "") to go the other direction. If this is a typed table, only String columns will be modified.- Parameters:
orig
-replacement
-
-
replace
-
replace
-
replaceAll
-
replaceAll
-
replaceAll
Run String.replaceAll() on all entries in a column. Only works with columns that are already String values.- Parameters:
regex
- the String to matchcolumnName
- title of the column to search
-
removeTokens
Removes any of the specified characters (or "tokens"). The example above removes all commas, dollar signs, and spaces from the table.
If no column is specified, then the values in all columns and rows are processed. A specific column may be referenced by either its ID or title.- Parameters:
tokens
- a list of individual characters to be removed- See Also:
-
removeTokens
Removed any of the specified characters from a column. For instance, the following code removes dollar signs and commas from column 2:table.removeTokens(",$", 2);
- Parameters:
column
- ID number of the column to process
-
removeTokens
- Parameters:
columnName
- title of the column to process
-
trim
public void trim()Trims leading and trailing whitespace, such as spaces and tabs, from String table values. If no column is specified, then the values in all columns and rows are trimmed. A specific column may be referenced by either its ID or title.- See Also:
-
trim
public void trim(int column) - Parameters:
column
- ID number of the column to trim
-
trim
- Parameters:
columnName
- title of the column to trim
-
sort
Sorts (orders) a table based on the values in a column.- Parameters:
columnName
- the name of the column to sort- See Also:
-
sort
public void sort(int column) - Parameters:
column
- the column ID, e.g. 0, 1, 2
-
sortReverse
-
sortReverse
public void sortReverse(int column) -
getUnique
-
getUnique
-
getTally
-
getTally
-
getOrder
-
getOrder
-
getIntList
-
getIntList
-
getFloatList
-
getFloatList
-
getStringList
-
getStringList
-
getIntDict
-
getIntDict
-
getFloatDict
-
getFloatDict
-
getStringDict
-
getStringDict
-
getRowMap
-
getRowMap
Return a mapping that connects the entry from a column back to the row from which it came. For instance:Table t = loadTable("country-data.tsv", "header"); // use the contents of the 'country' column to index the table Map<String, TableRow> lookup = t.getRowMap("country"); // get the row that has "us" in the "country" column: TableRow usRow = lookup.get("us"); // get an entry from the 'population' column int population = usRow.getInt("population");
-
copy
Make a copy of the current table -
write
-
print
public void print()
-