- java.lang.Object
-
- processing.data.Table
-
public class Table extends Object
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
Fields Modifier and Type Field Description protected intallocCountstatic intCATEGORYprotected Object[]columnsstatic intDOUBLEprotected intexpandIncrementstatic intFLOATstatic intINTstatic intLONGprotected intmissingCategoryprotected doublemissingDoubleprotected floatmissingFloatprotected intmissingIntprotected longmissingLongprotected StringmissingStringprotected introwCountprotected processing.data.Table.RowIteratorrowIteratorstatic intSTRING
-
Constructor Summary
Constructors Constructor Description Table()Creates a new, empty table.Table(File file)Table(File file, String options)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.Table(Iterable<TableRow> rows)Table(ResultSet rs)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddColumn()voidaddColumn(String title)voidaddColumn(String title, int type)TableRowaddRow()TableRowaddRow(Object[] columnData)TableRowaddRow(TableRow source)voidaddRows(Table source)protected voidcheckBounds(int row, int column)Throw an error if this entry is out of bounds.protected voidcheckColumn(int column)Throw an error if this column doesn't exist.intcheckColumnIndex(String title)Same as getColumnIndex(), but creates the column if it doesn't exist.protected voidcheckRow(int row)Throw an error if this row doesn't exist.voidclearRows()protected voidconvertBasic(BufferedReader reader, boolean tsv, File outputFile)protected voidconvertRow(DataOutputStream output, String[] pieces)Tablecopy()Make a copy of the current tableprotected TablecreateSubset(int[] rowSubset)protected voidensureBounds(int row, int col)Make sure this is a legit row and column.protected voidensureColumn(int col)Make sure this is a legit column, and if not, expand the table.protected voidensureRow(int row)Make sure this is a legit row, and if not, expand the table.static StringextensionOptions(boolean loading, String filename, String options)TableRowfindRow(String value, int column)TableRowfindRow(String value, String columnName)intfindRowIndex(String value, int column)Return the row that contains the first String that matches.intfindRowIndex(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.Iterator<TableRow>findRowIterator(String value, int column)Iterator<TableRow>findRowIterator(String value, String columnName)Iterable<TableRow>findRows(String value, int column)Iterable<TableRow>findRows(String value, String columnName)intgetColumnCount()intgetColumnIndex(String columnName)protected intgetColumnIndex(String name, boolean report)Get the index of a column.StringgetColumnTitle(int col)String[]getColumnTitles()intgetColumnType(int column)Returns one of Table.STRING, Table.INT, etc...intgetColumnType(String columnName)int[]getColumnTypes()doublegetDouble(int row, int column)doublegetDouble(int row, String columnName)double[]getDoubleColumn(int col)double[]getDoubleColumn(String name)double[]getDoubleRow(int row)floatgetFloat(int row, int column)Get a float value from the specified row and column.floatgetFloat(int row, String columnName)float[]getFloatColumn(int col)float[]getFloatColumn(String name)FloatDictgetFloatDict(int keyColumn, int valueColumn)FloatDictgetFloatDict(String keyColumnName, String valueColumnName)FloatListgetFloatList(int column)FloatListgetFloatList(String columnName)float[]getFloatRow(int row)intgetInt(int row, int column)intgetInt(int row, String columnName)int[]getIntColumn(int col)int[]getIntColumn(String name)IntDictgetIntDict(int keyColumn, int valueColumn)IntDictgetIntDict(String keyColumnName, String valueColumnName)IntListgetIntList(int column)IntListgetIntList(String columnName)int[]getIntRow(int row)longgetLong(int row, int column)longgetLong(int row, String columnName)long[]getLongColumn(int col)long[]getLongColumn(String name)long[]getLongRow(int row)protected floatgetMaxFloat()Searches the entire table for float values.IntDictgetOrder(int column)IntDictgetOrder(String columnName)TableRowgetRow(int row)intgetRowCount()Map<String,TableRow>getRowMap(int column)Return a mapping that connects the entry from a column back to the row from which it came.Map<String,TableRow>getRowMap(String columnName)StringgetString(int row, int column)Get a String value from the table.StringgetString(int row, String columnName)String[]getStringColumn(int column)String[]getStringColumn(String columnName)StringDictgetStringDict(int keyColumn, int valueColumn)StringDictgetStringDict(String keyColumnName, String valueColumnName)StringListgetStringList(int column)StringListgetStringList(String columnName)String[]getStringRow(int row)IntDictgetTally(int column)IntDictgetTally(String columnName)String[]getUnique(int column)String[]getUnique(String columnName)booleanhasColumnTitles()protected voidinit()voidinsertColumn(int index)voidinsertColumn(int index, String title)voidinsertColumn(int index, String title, int type)voidinsertRow(int insert, Object[] columnData)protected booleanisEmptyArray(String[] contents)intlastRowIndex()protected voidloadBinary(InputStream is)TableRowmatchRow(String regexp, int column)TableRowmatchRow(String regexp, String columnName)intmatchRowIndex(String regexp, int column)Return the row that contains the first String that matches.intmatchRowIndex(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.Iterator<TableRow>matchRowIterator(String value, int column)Iterator<TableRow>matchRowIterator(String value, String columnName)Iterable<TableRow>matchRows(String regexp, int column)Iterable<TableRow>matchRows(String regexp, String columnName)protected voidodsParse(InputStream input, String worksheet, boolean header)protected voidparse(InputStream input, String options)protected voidparseBasic(BufferedReader reader, boolean header, boolean tsv)voidparseInto(Object enclosingObject, String fieldName)incomplete, do not usevoidprint()voidremoveColumn(int column)voidremoveColumn(String columnName)voidremoveRow(int row)String[]removeTitleRow()Deprecated.voidremoveTokens(String tokens)Remove any of the specified characters from the entire table.voidremoveTokens(String tokens, int column)Removed any of the specified characters from a column.voidremoveTokens(String tokens, String columnName)voidreplace(String orig, String replacement)Replace a String with another.voidreplace(String orig, String replacement, int col)voidreplace(String orig, String replacement, String colName)voidreplaceAll(String regex, String replacement)voidreplaceAll(String regex, String replacement, int column)voidreplaceAll(String regex, String replacement, String columnName)Run String.replaceAll() on all entries in a column.Iterable<TableRow>rows()Note that this one iterator instance is shared by any calls to iterate the rows of this table.Iterable<TableRow>rows(int[] indices)booleansave(File file, String options)booleansave(OutputStream output, String options)protected voidsaveBinary(OutputStream os)protected voidsaveODS(OutputStream os)voidsetColumnCount(int newCount)Change the number of columns in this table.voidsetColumnTitle(int column, String title)voidsetColumnTitles(String[] titles)voidsetColumnType(int column, int newType)Sets the column type.voidsetColumnType(int column, String columnType)Set the data type for a column so that using it is more efficient.voidsetColumnType(String columnName, int newType)voidsetColumnType(String columnName, String columnType)voidsetColumnTypes(int[] types)voidsetColumnTypes(Table dictionary)Set the titles (and if a second column is present) the data types for this table based on a file loaded separately.voidsetDouble(int row, int column, double value)voidsetDouble(int row, String columnName, double value)voidsetFloat(int row, int column, float value)voidsetFloat(int row, String columnName, float value)voidsetInt(int row, int column, int value)voidsetInt(int row, String columnName, int value)voidsetLong(int row, int column, long value)voidsetLong(int row, String columnName, long value)voidsetMissingDouble(double value)voidsetMissingFloat(float value)voidsetMissingInt(int value)voidsetMissingLong(long value)voidsetMissingString(String value)Treat entries with this string as "missing".voidsetRow(int row, Object[] pieces)TableRowsetRow(int row, TableRow source)protected voidsetRowCol(int row, int col, Object piece)voidsetRowCount(int newCount)voidsetString(int row, int column, String value)voidsetString(int row, String columnName, String value)voidsetTableType(String type)Set the entire table to a specific data type.voidsort(int column)protected voidsort(int column, boolean reverse)voidsort(String columnName)Sorts (orders) a table based on the values in a column.voidsortReverse(int column)voidsortReverse(String columnName)protected String[]splitLineCSV(String line, BufferedReader reader)Parse a line of text as comma-separated values, returning each value as one entry in an array of String objects.voidtrim()voidtrim(int column)voidtrim(String columnName)TabletypedParse(InputStream input, String options)voidwrite(PrintWriter writer)protected voidwriteCSV(PrintWriter writer)protected voidwriteEntryCSV(PrintWriter writer, String entry)protected voidwriteEntryHTML(PrintWriter writer, String entry)protected voidwriteHTML(PrintWriter writer)protected voidwriteTSV(PrintWriter writer)
-
-
-
Field Detail
-
rowCount
protected int rowCount
-
allocCount
protected int allocCount
-
missingString
protected String missingString
-
missingInt
protected int missingInt
-
missingLong
protected long missingLong
-
missingFloat
protected float missingFloat
-
missingDouble
protected double missingDouble
-
missingCategory
protected int missingCategory
-
columns
protected Object[] columns
-
STRING
public static final int STRING
- See Also:
- Constant Field Values
-
INT
public static final int INT
- See Also:
- Constant Field Values
-
LONG
public static final int LONG
- See Also:
- Constant Field Values
-
FLOAT
public static final int FLOAT
- See Also:
- Constant Field Values
-
DOUBLE
public static final int DOUBLE
- See Also:
- Constant Field Values
-
CATEGORY
public static final int CATEGORY
- See Also:
- Constant Field Values
-
rowIterator
protected processing.data.Table.RowIterator rowIterator
-
expandIncrement
protected int expandIncrement
-
-
Constructor Detail
-
Table
public Table()
Creates a new, empty table. Use addRow() to add additional rows.
-
Table
public Table(File file) throws IOException
- Throws:
IOException
-
Table
public Table(File file, String options) throws IOException
version that uses a File object; future releases (or data types) may include additional optimizations here- Throws:
IOException
-
Table
public Table(InputStream input) throws IOException
- Throws:
IOException
-
Table
public Table(InputStream input, String options) throws IOException
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
public Table(ResultSet rs)
-
-
Method Detail
-
typedParse
public Table typedParse(InputStream input, String options) throws IOException
- Throws:
IOException
-
init
protected void init()
-
extensionOptions
public static String extensionOptions(boolean loading, String filename, String options)
-
parse
protected void parse(InputStream input, String options) throws IOException
- Throws:
IOException
-
parseBasic
protected void parseBasic(BufferedReader reader, boolean header, boolean tsv) throws IOException
- Throws:
IOException
-
splitLineCSV
protected String[] splitLineCSV(String line, BufferedReader reader) throws IOException
Parse a line of text as comma-separated values, returning each value as one entry in an array of String objects. Remove quotes from entries that begin and end with them, and convert 'escaped' quotes to actual quotes.- Parameters:
line- line of text to be parsed- Returns:
- an array of the individual values formerly separated by commas
- Throws:
IOException
-
odsParse
protected void odsParse(InputStream input, String worksheet, boolean header)
-
save
public boolean save(File file, String options) throws IOException
- Throws:
IOException
-
save
public boolean save(OutputStream output, String options)
-
writeTSV
protected void writeTSV(PrintWriter writer)
-
writeCSV
protected void writeCSV(PrintWriter writer)
-
writeEntryCSV
protected void writeEntryCSV(PrintWriter writer, String entry)
-
writeHTML
protected void writeHTML(PrintWriter writer)
-
writeEntryHTML
protected void writeEntryHTML(PrintWriter writer, String entry)
-
saveODS
protected void saveODS(OutputStream os) throws IOException
- Throws:
IOException
-
saveBinary
protected void saveBinary(OutputStream os) throws IOException
- Throws:
IOException
-
loadBinary
protected void loadBinary(InputStream is) throws IOException
- Throws:
IOException
-
addColumn
public void addColumn()
- See Also:
removeColumn(String)
-
addColumn
public void addColumn(String title)
- Parameters:
title- the title to be used for the new column
-
addColumn
public void addColumn(String title, int type)
- Parameters:
type- the type to be used for the new column: INT, LONG, FLOAT, DOUBLE, or STRING
-
insertColumn
public void insertColumn(int index)
-
insertColumn
public void insertColumn(int index, String title)
-
insertColumn
public void insertColumn(int index, String title, int type)
-
removeColumn
public void removeColumn(String columnName)
- Parameters:
columnName- the title of the column to be removed- See Also:
addColumn()
-
removeColumn
public void removeColumn(int column)
- Parameters:
column- the index number of the column to be removed
-
getColumnCount
public int getColumnCount()
- See Also:
getRowCount()
-
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
public void setColumnType(int column, String columnType)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
public void setColumnType(String columnName, int newType)
-
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
public void setTableType(String type)
Set the entire table to a specific data type.
-
setColumnTypes
public void setColumnTypes(int[] types)
-
setColumnTypes
public 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. 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
public int getColumnType(String columnName)
-
getColumnType
public int getColumnType(int column)
Returns one of Table.STRING, Table.INT, etc...
-
getColumnTypes
public int[] getColumnTypes()
-
removeTitleRow
@Deprecated public String[] removeTitleRow()
Deprecated.Remove the first row from the data set, and use it as the column titles. Use loadTable("table.csv", "header") instead.
-
setColumnTitles
public void setColumnTitles(String[] titles)
-
setColumnTitle
public void setColumnTitle(int column, String title)
-
hasColumnTitles
public boolean hasColumnTitles()
-
getColumnTitles
public String[] getColumnTitles()
-
getColumnTitle
public String getColumnTitle(int col)
-
getColumnIndex
public int getColumnIndex(String columnName)
-
getColumnIndex
protected int getColumnIndex(String name, boolean report)
Get the index of a column.- Parameters:
name- Name of the column.report- Whether to throw an exception if the column wasn't found.- Returns:
- index of the found column, or -1 if not found.
-
checkColumnIndex
public int checkColumnIndex(String title)
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()
- See Also:
getColumnCount()
-
lastRowIndex
public int lastRowIndex()
-
clearRows
public void clearRows()
- See Also:
addRow(),removeRow(int)
-
setRowCount
public void setRowCount(int newCount)
-
addRow
public TableRow addRow()
- See Also:
removeRow(int),clearRows()
-
addRow
public TableRow addRow(TableRow source)
- Parameters:
source- a reference to the original row to be duplicated
-
addRows
public void addRows(Table source)
-
insertRow
public void insertRow(int insert, Object[] columnData)
-
removeRow
public void removeRow(int row)
- Parameters:
row- ID number of the row to remove- See Also:
addRow(),clearRows()
-
setRow
public void setRow(int row, Object[] pieces)
-
setRowCol
protected void setRowCol(int row, int col, Object piece)
-
getRow
public TableRow getRow(int row)
- Parameters:
row- ID number of the row to get- See Also:
rows(),findRow(String, int),findRows(String, int),matchRow(String, int),matchRows(String, int)
-
rows
public Iterable<TableRow> rows()
Note that this one iterator instance is shared by any calls to iterate the rows of this table. This is very efficient, but not thread-safe. If you want to iterate in a multi-threaded manner, don't use the iterator.
-
getInt
public int getInt(int row, int column)- Parameters:
row- ID number of the row to referencecolumn- ID number of the column to reference- See Also:
getFloat(int, int),getString(int, int),getStringColumn(String),setInt(int, int, int),setFloat(int, int, float),setString(int, int, String)
-
getInt
public int getInt(int row, String columnName)- Parameters:
columnName- title of the column to reference
-
setMissingInt
public void setMissingInt(int value)
-
setInt
public void setInt(int row, int column, int value)- Parameters:
row- ID number of the target rowcolumn- ID number of the target columnvalue- value to assign- See Also:
setFloat(int, int, float),setString(int, int, String),getInt(int, int),getFloat(int, int),getString(int, int),getStringColumn(String)
-
setInt
public void setInt(int row, String columnName, int value)- Parameters:
columnName- title of the target column
-
getIntColumn
public int[] getIntColumn(String name)
-
getIntColumn
public int[] getIntColumn(int col)
-
getIntRow
public int[] getIntRow(int row)
-
getLong
public long getLong(int row, int column)
-
getLong
public long getLong(int row, String columnName)
-
setMissingLong
public void setMissingLong(long value)
-
setLong
public void setLong(int row, int column, long value)
-
setLong
public void setLong(int row, String columnName, long value)
-
getLongColumn
public long[] getLongColumn(String name)
-
getLongColumn
public long[] getLongColumn(int col)
-
getLongRow
public long[] getLongRow(int row)
-
getFloat
public float getFloat(int row, int column)Get a float value from the specified row and column. If the value is null or not parseable as a float, the "missing" value is returned. By default, this is Float.NaN, but can be controlled with setMissingFloat().- Parameters:
row- ID number of the row to referencecolumn- ID number of the column to reference- See Also:
getInt(int, int),getString(int, int),getStringColumn(String),setInt(int, int, int),setFloat(int, int, float),setString(int, int, String)
-
getFloat
public float getFloat(int row, String columnName)- Parameters:
columnName- title of the column to reference
-
setMissingFloat
public void setMissingFloat(float value)
-
setFloat
public void setFloat(int row, int column, float value)- Parameters:
row- ID number of the target rowcolumn- ID number of the target columnvalue- value to assign- See Also:
setInt(int, int, int),setString(int, int, String),getInt(int, int),getFloat(int, int),getString(int, int),getStringColumn(String)
-
setFloat
public void setFloat(int row, String columnName, float value)- Parameters:
columnName- title of the target column
-
getFloatColumn
public float[] getFloatColumn(String name)
-
getFloatColumn
public float[] getFloatColumn(int col)
-
getFloatRow
public float[] getFloatRow(int row)
-
getDouble
public double getDouble(int row, int column)
-
getDouble
public double getDouble(int row, String columnName)
-
setMissingDouble
public void setMissingDouble(double value)
-
setDouble
public void setDouble(int row, int column, double value)
-
setDouble
public void setDouble(int row, String columnName, double value)
-
getDoubleColumn
public double[] getDoubleColumn(String name)
-
getDoubleColumn
public double[] getDoubleColumn(int col)
-
getDoubleRow
public double[] getDoubleRow(int row)
-
getString
public String getString(int row, int column)
Get a String value from the table. If the row is longer than the table- Parameters:
row- ID number of the row to referencecolumn- ID number of the column to reference- See Also:
getInt(int, int),getFloat(int, int),getStringColumn(String),setInt(int, int, int),setFloat(int, int, float),setString(int, int, String)
-
getString
public String getString(int row, String columnName)
- Parameters:
columnName- title of the column to reference
-
setMissingString
public void setMissingString(String value)
Treat entries with this string as "missing". Also used for categorial.
-
setString
public void setString(int row, int column, String value)- Parameters:
row- ID number of the target rowcolumn- ID number of the target columnvalue- value to assign- See Also:
setInt(int, int, int),setFloat(int, int, float),getInt(int, int),getFloat(int, int),getString(int, int),getStringColumn(String)
-
setString
public void setString(int row, String columnName, String value)- Parameters:
columnName- title of the target column
-
getStringColumn
public String[] getStringColumn(String columnName)
- Parameters:
columnName- title of the column to search- See Also:
getInt(int, int),getFloat(int, int),getString(int, int),setInt(int, int, int),setFloat(int, int, float),setString(int, int, String)
-
getStringColumn
public String[] getStringColumn(int column)
- Parameters:
column- ID number of the column to search
-
getStringRow
public String[] getStringRow(int row)
-
findRowIndex
public int findRowIndex(String value, int column)
Return the row that contains the first String that matches.- Parameters:
value- the String to matchcolumn- ID number of the column to search
-
findRowIndex
public int findRowIndex(String value, String columnName)
Return the row that contains the first String that matches.- Parameters:
value- the String to matchcolumnName- title of the column to search
-
findRowIndices
public int[] findRowIndices(String value, int column)
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
public int[] findRowIndices(String value, String columnName)
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
public TableRow findRow(String value, int column)
- Parameters:
value- the value to matchcolumn- ID number of the column to search- See Also:
getRow(int),rows(),findRows(String, int),matchRow(String, int),matchRows(String, int)
-
findRow
public TableRow findRow(String value, String columnName)
- Parameters:
columnName- title of the column to search
-
findRows
public Iterable<TableRow> findRows(String value, int column)
- Parameters:
value- the value to matchcolumn- ID number of the column to search- See Also:
getRow(int),rows(),findRow(String, int),matchRow(String, int),matchRows(String, int)
-
findRows
public Iterable<TableRow> findRows(String value, String columnName)
- Parameters:
columnName- title of the column to search
-
findRowIterator
public Iterator<TableRow> findRowIterator(String value, int column)
- Parameters:
value- the value to matchcolumn- ID number of the column to search
-
findRowIterator
public Iterator<TableRow> findRowIterator(String value, String columnName)
- Parameters:
columnName- title of the column to search
-
matchRowIndex
public int matchRowIndex(String regexp, int column)
Return the row that contains the first String that matches.- Parameters:
regexp- the String to matchcolumn- ID number of the column to search
-
matchRowIndex
public int matchRowIndex(String what, String columnName)
Return the row that contains the first String that matches.- Parameters:
what- the String to matchcolumnName- title of the column to search
-
matchRowIndices
public int[] matchRowIndices(String regexp, int column)
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
public int[] matchRowIndices(String what, String columnName)
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
public TableRow matchRow(String regexp, int column)
- Parameters:
regexp- the regular expression to matchcolumn- ID number of the column to search- See Also:
getRow(int),rows(),findRow(String, int),findRows(String, int),matchRows(String, int)
-
matchRow
public TableRow matchRow(String regexp, String columnName)
- Parameters:
columnName- title of the column to search
-
matchRows
public Iterable<TableRow> matchRows(String regexp, int column)
- Parameters:
regexp- the regular expression to matchcolumn- ID number of the column to search- See Also:
getRow(int),rows(),findRow(String, int),findRows(String, int),matchRow(String, int)
-
matchRows
public Iterable<TableRow> matchRows(String regexp, String columnName)
- Parameters:
columnName- title of the column to search
-
matchRowIterator
public Iterator<TableRow> matchRowIterator(String value, int column)
- Parameters:
value- the regular expression to matchcolumn- ID number of the column to search
-
matchRowIterator
public Iterator<TableRow> matchRowIterator(String value, String columnName)
- Parameters:
columnName- title of the column to search
-
replace
public void replace(String orig, String replacement)
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-
-
replaceAll
public void replaceAll(String regex, String replacement, String columnName)
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
public void removeTokens(String tokens)
Remove any of the specified characters from the entire table.- Parameters:
tokens- a list of individual characters to be removed- See Also:
trim()
-
removeTokens
public void removeTokens(String tokens, int column)
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
public void removeTokens(String tokens, String columnName)
- Parameters:
columnName- title of the column to process
-
trim
public void trim()
- See Also:
removeTokens(String)
-
isEmptyArray
protected boolean isEmptyArray(String[] contents)
-
trim
public void trim(int column)
- Parameters:
column- ID number of the column to trim
-
trim
public void trim(String columnName)
- Parameters:
columnName- title of the column to trim
-
ensureColumn
protected void ensureColumn(int col)
Make sure this is a legit column, and if not, expand the table.
-
ensureRow
protected void ensureRow(int row)
Make sure this is a legit row, and if not, expand the table.
-
ensureBounds
protected void ensureBounds(int row, int col)Make sure this is a legit row and column. If not, expand the table.
-
checkRow
protected void checkRow(int row)
Throw an error if this row doesn't exist.
-
checkColumn
protected void checkColumn(int column)
Throw an error if this column doesn't exist.
-
checkBounds
protected void checkBounds(int row, int column)Throw an error if this entry is out of bounds.
-
sort
public void sort(String columnName)
Sorts (orders) a table based on the values in a column.- Parameters:
columnName- the name of the column to sort- See Also:
trim()
-
sort
public void sort(int column)
- Parameters:
column- the column ID, e.g. 0, 1, 2
-
sortReverse
public void sortReverse(String columnName)
-
sortReverse
public void sortReverse(int column)
-
sort
protected void sort(int column, boolean reverse)
-
getUnique
public String[] getUnique(int column)
-
getTally
public IntDict getTally(int column)
-
getOrder
public IntDict getOrder(int column)
-
getIntList
public IntList getIntList(int column)
-
getFloatList
public FloatList getFloatList(int column)
-
getStringList
public StringList getStringList(String columnName)
-
getStringList
public StringList getStringList(int column)
-
getIntDict
public IntDict getIntDict(int keyColumn, int valueColumn)
-
getFloatDict
public FloatDict getFloatDict(int keyColumn, int valueColumn)
-
getStringDict
public StringDict getStringDict(String keyColumnName, String valueColumnName)
-
getStringDict
public StringDict getStringDict(int keyColumn, int valueColumn)
-
getRowMap
public Map<String,TableRow> getRowMap(int column)
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 Maplookup = 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");
-
createSubset
protected Table createSubset(int[] rowSubset)
-
getMaxFloat
protected float getMaxFloat()
Searches the entire table for float values. Returns missing float (Float.NaN by default) if no valid numbers found.
-
convertBasic
protected void convertBasic(BufferedReader reader, boolean tsv, File outputFile) throws IOException
- Throws:
IOException
-
convertRow
protected void convertRow(DataOutputStream output, String[] pieces) throws IOException
- Throws:
IOException
-
copy
public Table copy()
Make a copy of the current table
-
write
public void write(PrintWriter writer)
-
print
public void print()
-
-