Theoretica
Mathematical Library
Loading...
Searching...
No Matches
theoretica::data_table Class Reference

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.
 
realat (const std::string &col, size_t row)
 Access an element by row index and column name, returning a reference to the value.
 
const realat (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, realrow (size_t idx) const
 Get an entire row as a map of column names to values.
 
vec< realrow_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_tableinsert (const std::string &name, const vec< real > &data)
 Insert a new column into the data table with the given name and data.
 
data_tableinsert (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_tabledrop_column (const std::string &name)
 Drop a column from the data table by name.
 
data_tabledrop_columns (const std::vector< std::string > &names)
 Drop multiple columns from the data table by name.
 
data_tablerename (const std::string &old_name, const std::string &new_name)
 Rename a column in the data table from old_name to new_name.
 
mat< realto_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_tablefrom_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)
 

Detailed Description

A data structure for holding labeled columns of data, where each column is a vector of real numbers.

Constructor & Destructor Documentation

◆ data_table() [1/2]

theoretica::data_table::data_table ( const std::map< std::string, vec< real > > &  table)
inline

Construct from map of column vectors.

The order of columns is determined by the order of the map iteration.

Parameters
tableA map where keys are column names and values are column vectors.

◆ data_table() [2/2]

theoretica::data_table::data_table ( size_t  num_rows,
const std::vector< std::string > &  column_names 
)
inline

Construct a data table with preallocated size.

Parameters
num_rowsThe number of rows in the data table
column_namesThe names of the columns in the data table

Member Function Documentation

◆ at() [1/3]

real & theoretica::data_table::at ( const std::string &  col,
size_t  row 
)
inline

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.

Parameters
rowThe index of the row to access.
colThe name of the column to access.
Returns
A reference to the value at the given row and column.

◆ at() [2/3]

const real & theoretica::data_table::at ( const std::string &  col,
size_t  row 
) const
inline

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.

Parameters
rowThe index of the row to access.
colThe name of the column to access.
Returns
A const reference to the value at the given row and column.

◆ at() [3/3]

vec< real > & theoretica::data_table::at ( const std::string &  name)
inline

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.

Parameters
nameThe name of the column to access.
Returns
A reference to the column vector with the given name.

◆ cols()

size_t theoretica::data_table::cols ( ) const
inline

Get the number of columns in the data table.

Returns
The number of columns in the data table.

◆ data() [1/2]

std::vector< vec< real > > & theoretica::data_table::data ( )
inline

Get the columns of the data table as a simple vector of column vectors, without the column names or indices, by reference.

Returns
A vector of column vectors in the data table.

◆ data() [2/2]

const std::vector< vec< real > > & theoretica::data_table::data ( ) const
inline

Get the columns of the data table as a simple vector of column vectors, without the column names or indices.

Returns
A vector of column vectors in the data table.

◆ drop_column()

data_table & theoretica::data_table::drop_column ( const std::string &  name)
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.

Parameters
nameThe name of the column to drop.

◆ drop_columns()

data_table & theoretica::data_table::drop_columns ( const std::vector< std::string > &  names)
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.

Parameters
namesThe names of the columns to drop.

◆ empty()

bool theoretica::data_table::empty ( ) const
inline

Check whether the data table is empty (i.e.

has no columns or all columns are empty).

Returns
True if the data table is empty, false otherwise.

◆ from_matrix()

data_table & theoretica::data_table::from_matrix ( const mat< real > &  m,
const std::vector< std::string > &  col_names 
)
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.

Parameters
mThe matrix to read elements from.
col_namesNames of the columns in the resulting data table.

◆ has_column()

bool theoretica::data_table::has_column ( const std::string &  name) const
inline

Check whether the data table has a column with the given name.

Parameters
nameThe name of the column to check for.
Returns
Whether the table contains a column with the given name.

◆ head()

data_table theoretica::data_table::head ( size_t  n = 5) const
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.

Parameters
nThe number of rows to include in the head of the data table.
Returns
A new data table containing the first n rows of the original table.

◆ header()

std::vector< std::string > theoretica::data_table::header ( ) const
inline

Get a list of the column names in the data table.

Returns
A vector of column names in the data table.

◆ insert() [1/2]

data_table & theoretica::data_table::insert ( const std::string &  name,
const vec< real > &  data 
)
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.

Parameters
nameThe name of the column to add.
dataThe data representing the new column.

◆ insert() [2/2]

data_table & theoretica::data_table::insert ( const std::string &  name,
size_t  num_rows,
real  value = 0.0 
)
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.

Parameters
nameThe name of the column to add.
num_rowsThe number of rows in the new column.
valueThe value to fill the new column with.

◆ operator[]() [1/3]

vec< real > & theoretica::data_table::operator[] ( const std::string &  name)
inline

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.

Parameters
nameThe name of the column to access.
Returns
A reference to the column vector with the given name.

◆ operator[]() [2/3]

const vec< real > & theoretica::data_table::operator[] ( const std::string &  name) const
inline

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.

Parameters
nameThe name of the column to access.
Returns
A const reference to the column vector with the given name.

◆ operator[]() [3/3]

vec< real > & theoretica::data_table::operator[] ( size_t  idx)
inline

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.

Parameters
idxThe index of the column to access.
Returns
A reference to the column vector at the given index.

◆ rename()

data_table & theoretica::data_table::rename ( const std::string &  old_name,
const std::string &  new_name 
)
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.

Parameters
old_nameThe old name of the column to rename
new_nameThe new name of the column

◆ row()

std::unordered_map< std::string, real > theoretica::data_table::row ( size_t  idx) const
inline

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.

Parameters
idxThe index of the row to access.
Returns
A map of column names to values for the given row.

◆ row_vec()

vec< real > theoretica::data_table::row_vec ( size_t  idx) const
inline

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.

Parameters
idxThe index of the row to access.
Returns
A map of column names to values for the given row.

◆ rows()

size_t theoretica::data_table::rows ( ) const
inline

Get the (maximum) number of rows in the data table.

This operation is O(K) in the number of columns K.

Returns
The size of the first column or 0 if the table is empty.

◆ select()

data_table theoretica::data_table::select ( const std::vector< std::string > &  cols) const
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.

Note
The current implementation immediately returns a new data table, but in the future it may be optimized to return a view of the original data table instead, to avoid unnecessary copying.
Parameters
colsA vector of column names to select from the data table.
Returns
A new data table containing only the selected columns.

◆ size()

size_t theoretica::data_table::size ( ) const
inline

Get the total number of elements in the data table, i.e.

N. rows * N. columns.

Returns
The total number of elements in the data table.

◆ tail()

data_table theoretica::data_table::tail ( size_t  n = 5) const
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.

Parameters
nThe number of rows to include in the tail of the data table.
Returns
A new data table containing the last n rows of the original table.

◆ to_matrix()

mat< real > theoretica::data_table::to_matrix ( ) const
inline

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.

Returns
A matrix representation of the data table.

The documentation for this class was generated from the following file: