|
Theoretica
Mathematical Library
|
A data structure for holding labeled columns of data, where each column is a vector of real numbers. More...
#include <data_table.h>
Public Member Functions | |
| data_table () | |
| Default constructor. | |
| data_table (const std::map< std::string, vec< real > > &table) | |
| Construct from map of column vectors. | |
| data_table (size_t num_rows, const std::vector< std::string > &column_names) | |
| Construct a data table with preallocated size. | |
| data_table (const data_table &other) | |
| Copy constructor from another data table. | |
| data_table (data_table &&other) noexcept | |
| Move constructor from another data table, leaving the other empty. | |
| size_t | rows () const |
| Get the (maximum) number of rows in the data table. | |
| size_t | cols () const |
| Get the number of columns in the data table. | |
| size_t | size () const |
| Get the total number of elements in the data table, i.e. | |
| bool | empty () const |
| Check whether the data table is empty (i.e. | |
| void | clear () |
| Remove all columns from the data table, leaving it empty. | |
| std::vector< std::string > | header () const |
| Get a list of the column names in the data table. | |
| bool | has_column (const std::string &name) const |
| Check whether the data table has a column with the given name. | |
| std::vector< vec< real > > & | data () |
| Get the columns of the data table as a simple vector of column vectors, without the column names or indices, by reference. | |
| const std::vector< vec< real > > & | data () const |
| Get the columns of the data table as a simple vector of column vectors, without the column names or indices. | |
| vec< real > & | operator[] (const std::string &name) |
| Access a column by name, returning a reference to the column vector. | |
| const vec< real > & | operator[] (const std::string &name) const |
| Access a column by name, returning a const reference to the column vector. | |
| vec< real > & | at (const std::string &name) |
| Access a column by name, returning a reference to the column vector. | |
| vec< real > & | operator[] (size_t idx) |
| Access a column by index, returning a reference to the column vector. | |
| data_table | select (const std::vector< std::string > &cols) const |
| Select a subset of columns from the table, returning a new table containing only the selected columns. | |
| real & | at (const std::string &col, size_t row) |
| Access an element by row index and column name, returning a reference to the value. | |
| const real & | at (const std::string &col, size_t row) const |
| Access an element by row index and column name, returning a const reference to the value. | |
| std::unordered_map< std::string, real > | row (size_t idx) const |
| Get an entire row as a map of column names to values. | |
| vec< real > | row_vec (size_t idx) const |
| Get an entire row as a vector of values, in the same order as the columns in the data table. | |
| data_table | head (size_t n=5) const |
| Get the first n rows of the data table as a new data table. | |
| data_table | tail (size_t n=5) const |
| Get the last n rows of the data table as a new data table. | |
| data_table & | insert (const std::string &name, const vec< real > &data) |
| Insert a new column into the data table with the given name and data. | |
| data_table & | insert (const std::string &name, size_t num_rows, real value=0.0) |
| Insert a new column into the data table with the given name, number of rows, and constant value. | |
| data_table & | drop_column (const std::string &name) |
| Drop a column from the data table by name. | |
| data_table & | drop_columns (const std::vector< std::string > &names) |
| Drop multiple columns from the data table by name. | |
| data_table & | rename (const std::string &old_name, const std::string &new_name) |
| Rename a column in the data table from old_name to new_name. | |
| mat< real > | to_matrix () const |
| Convert the data table to a matrix, where each column of the matrix corresponds to a column in the data table, and each row corresponds to a row in the data table. | |
| data_table & | from_matrix (const mat< real > &m, const std::vector< std::string > &col_names) |
| Convert a matrix to a data table, where each column of the matrix corresponds to a column in the data table, and each row corresponds to a row in the data table. | |
| std::string | to_string (unsigned int max_rows=8, unsigned int precision=6, unsigned int max_width=12) const |
| Convert the data table to string representation. | |
| operator std::string () | |
| Convert the data table to string representation. | |
Friends | |
| std::ostream & | operator<< (std::ostream &out, const data_table &obj) |
| Stream the data table in string representation to an output stream (std::ostream) | |
A data structure for holding labeled columns of data, where each column is a vector of real numbers.
Construct from map of column vectors.
The order of columns is determined by the order of the map iteration.
| table | A map where keys are column names and values are column vectors. |
|
inline |
Construct a data table with preallocated size.
| num_rows | The number of rows in the data table |
| column_names | The names of the columns in the data table |
Access an element by row index and column name, returning a reference to the value.
Throws an out_of_range exception if the column name is not found or the row index is out of range.
| row | The index of the row to access. |
| col | The name of the column to access. |
Access an element by row index and column name, returning a const reference to the value.
Throws an out_of_range exception if the column name is not found or the row index is out of range.
| row | The index of the row to access. |
| col | The name of the column to access. |
Access a column by name, returning a reference to the column vector.
Throws an out_of_range exception if the column name is not found.
| name | The name of the column to access. |
|
inline |
Get the number of columns in the data table.
Get the columns of the data table as a simple vector of column vectors, without the column names or indices, by reference.
Get the columns of the data table as a simple vector of column vectors, without the column names or indices.
|
inline |
Drop a column from the data table by name.
If the column name is not found, the table is unchanged. This operation is O(K) in the number of columns K, since it requires updating the indices of all subsequent columns.
| name | The name of the column to drop. |
|
inline |
Drop multiple columns from the data table by name.
Column names that are not found are ignored. This operation is O(K) with respect to the number of columns K, since it requires updating the indices of all subsequent columns for each dropped column.
| names | The names of the columns to drop. |
|
inline |
Check whether the data table is empty (i.e.
has no columns or all columns are empty).
|
inline |
Convert a matrix to a data table, where each column of the matrix corresponds to a column in the data table, and each row corresponds to a row in the data table.
The column names are taken from the provided vector, and if there are fewer column names than columns in the matrix, the remaining columns are named "col_i" where i is the column index. If there are more column names than columns in the matrix, the extra column names are ignored.
| m | The matrix to read elements from. |
| col_names | Names of the columns in the resulting data table. |
Check whether the data table has a column with the given name.
| name | The name of the column to check for. |
|
inline |
Get the first n rows of the data table as a new data table.
If n is greater than the number of rows in the table, the entire table is returned.
| n | The number of rows to include in the head of the data table. |
|
inline |
Get a list of the column names in the data table.
|
inline |
Insert a new column into the data table with the given name and data.
If a column with the same name already exists, it is overwritten.
| name | The name of the column to add. |
| data | The data representing the new column. |
|
inline |
Insert a new column into the data table with the given name, number of rows, and constant value.
If a column with the same name already exists, it is overwritten.
| name | The name of the column to add. |
| num_rows | The number of rows in the new column. |
| value | The value to fill the new column with. |
Access a column by name, returning a reference to the column vector.
Throws an out_of_range exception if the column name is not found.
| name | The name of the column to access. |
Access a column by name, returning a const reference to the column vector.
Throws an out_of_range exception if the column name is not found.
| name | The name of the column to access. |
Access a column by index, returning a reference to the column vector.
Does not perform bounds checking, so the behavior is undefined if the index is out of range, for safer access consider using the at() method instead, which performs bounds checking.
| idx | The index of the column to access. |
|
inline |
Rename a column in the data table from old_name to new_name.
If old_name is not found, the table is unchanged, while if new_name already exists, it is overwritten.
| old_name | The old name of the column to rename |
| new_name | The new name of the column |
Get an entire row as a map of column names to values.
Missing values for columns that do not have enough rows are filled with NaN.
| idx | The index of the row to access. |
Get an entire row as a vector of values, in the same order as the columns in the data table.
Missing values for columns that do not have enough rows are filled with NaN.
| idx | The index of the row to access. |
|
inline |
Get the (maximum) number of rows in the data table.
This operation is O(K) in the number of columns K.
|
inline |
Select a subset of columns from the table, returning a new table containing only the selected columns.
If a column name in the selection list is not found in the data table, it is ignored.
| cols | A vector of column names to select from the data table. |
|
inline |
Get the total number of elements in the data table, i.e.
N. rows * N. columns.
|
inline |
Get the last n rows of the data table as a new data table.
If n is greater than the number of rows in the table, the entire table is returned.
| n | The number of rows to include in the tail of the data table. |
Convert the data table to a matrix, where each column of the matrix corresponds to a column in the data table, and each row corresponds to a row in the data table.
Missing values are filled with NaN.