42 #include "../Io/FileTools.h" 43 #include "../Text/TextTools.h" 44 #include "../Text/StringTokenizer.h" 58 for (
size_t i = 0; i < nCol; i++)
60 data_[i].resize(nRow);
74 nCol_(colNames.size()),
75 data_(colNames.size()),
80 setColumnNames(colNames);
130 if (colIndex >= nCol_)
132 if (rowIndex >= data_[colIndex].size())
134 return data_[colIndex][rowIndex];
139 if (colIndex >= nCol_)
141 if (rowIndex >= data_[colIndex].size())
143 return data_[colIndex][rowIndex];
151 if (rowNames_ == NULL)
153 if (colNames_ == NULL)
159 return (*
this)(rowIndex, colIndex);
170 if (rowNames_ == NULL)
172 if (colNames_ == NULL)
178 return (*
this)(rowIndex, colIndex);
191 if (rowNames_ == NULL)
193 if (colIndex >= nCol_)
198 return (*
this)(rowIndex, colIndex);
209 if (rowNames_ == NULL)
211 if (colIndex >= nCol_)
216 return (*
this)(rowIndex, colIndex);
229 if (colNames_ == NULL)
234 if (rowIndex >= data_[colIndex].size())
235 throw IndexOutOfBoundsException(
"DataTable::operator(size_t, const string &).", rowIndex, 0, data_[colIndex].size() - 1);
236 return (*
this)(rowIndex, colIndex);
247 if (colNames_ == NULL)
252 if (rowIndex >= data_[colIndex].size())
253 throw IndexOutOfBoundsException(
"DataTable::operator(size_t, const string &).", rowIndex, 0, data_[colIndex].size() - 1);
254 return (*
this)(rowIndex, colIndex);
273 if (rowNames.size() != nRow_)
277 if (rowNames_ != NULL)
279 rowNames_ =
new vector<string>(rowNames.begin(), rowNames.end());
292 if (rowNames_ == NULL)
296 return (*rowNames_)[index];
306 if (colNames.size() != nCol_)
310 if (colNames_ != NULL)
312 colNames_ =
new vector<string>(colNames.begin(), colNames.end());
325 if (colNames_ == NULL)
329 return (*colNames_)[index];
355 if (colNames_ == NULL)
360 return data_[colIndex];
371 if (colNames_ == NULL)
376 return data_[colIndex];
388 for (
size_t i = 0; i <
colNames_->size(); i++)
401 data_.erase(data_.begin() +
static_cast<ptrdiff_t
>(index));
403 colNames_->erase(colNames_->begin() +
static_cast<ptrdiff_t
>(index));
415 data_.erase(data_.begin() +
static_cast<ptrdiff_t
>(colIndex));
416 colNames_->erase(colNames_->begin() +
static_cast<ptrdiff_t
>(colIndex));
430 if (newColumn.size() != nRow_)
432 data_.push_back(newColumn);
442 colNames_ =
new vector<string>();
446 if (newColumn.size() != nRow_)
448 if (nCol_ > 0 && find(colNames_->begin(), colNames_->end(), colName) != colNames_->end())
450 colNames_->push_back(colName);
451 data_.push_back(newColumn);
465 for (
size_t i = 0; i < nCol_; i++)
467 row.push_back(data_[i][index]);
481 for (
size_t i = 0; i < nCol_; i++)
483 row.push_back(data_[i][rowIndex]);
497 for (
size_t i = 0; i <
rowNames_->size(); i++)
508 for (
size_t j = 0; j < nCol_; j++)
510 vector<string>* column = &data_[j];
511 if (index >= column->size())
513 column->erase(column->begin() +
static_cast<ptrdiff_t
>(index));
516 rowNames_->erase(rowNames_->begin() +
static_cast<ptrdiff_t
>(index));
528 for (
size_t j = 0; j < nCol_; j++)
530 vector<string>* column = &data_[j];
531 column->erase(column->begin() +
static_cast<ptrdiff_t
>(rowIndex));
533 rowNames_->erase(rowNames_->begin() +
static_cast<ptrdiff_t
>(rowIndex));
547 if (newRow.size() != nCol_)
549 for (
size_t j = 0; j < nCol_; j++)
551 data_[j].push_back(newRow[j]);
562 rowNames_ =
new vector<string>();
566 if (newRow.size() != nCol_)
568 if (nRow_ > 0 && find(rowNames_->begin(), rowNames_->end(), rowName) != rowNames_->end())
570 rowNames_->push_back(rowName);
571 for (
size_t j = 0; j < nCol_; j++)
573 data_[j].push_back(newRow[j]);
591 size_t nCol = row1.size();
594 if (row1.size() == row2.size())
608 else if (row1.size() == row2.size() - 1)
612 string rowName = *row2.begin();
613 dt->
addRow(rowName, vector<string>(row2.begin() + 1, row2.end()));
617 throw DimensionException(
"DataTable::read(...). Row 2 has not the correct number of columns.", row2.size(), nCol);
626 string rowName = *st.
getTokens().begin();
641 if (static_cast<size_t>(rowNames) >= nCol)
642 throw IndexOutOfBoundsException(
"DataTable::read(...). Invalid column specified for row names.", static_cast<size_t>(rowNames), 0, nCol - 1);
643 vector<string> col = dt->
getColumn(static_cast<size_t>(rowNames));
664 for (
size_t i = 1; i < n; i++)
666 out << sep << colNames[i];
676 for (
size_t j = 1; j < n; j++)
678 out << sep << data(i, j);
695 for (
size_t i = 1; i < n; i++)
697 out << sep << colNames[i];
707 for (
size_t j = 1; j < n; j++)
709 out << sep << data(i, j);
std::vector< std::string > getColumnNames() const
Get the column names of this table.
This class allows to perform a correspondence analysis.
std::string getRowName(size_t index) const
Get a given row name.
Exception thrown when a given column name is not found is a DataTable object.
Exception thrown when attempting to duplicate a row name.
std::vector< std::string > * rowNames_
Exception thrown when trying to retrieve a row by its name and no row names have been specified...
Exception thrown when a given element was not found in the vector.
This class corresponds to a 'dataset', i.e. a table with data by rows and variable by columns...
void setRowNames(const std::vector< std::string > &rowNames)
Set the row names of this table.
void addColumn(const std::vector< std::string > &newColumn)
Add a new column.
void setColumnNames(const std::vector< std::string > &colNames)
Set the column names of this table.
std::vector< std::vector< std::string > > data_
std::vector< std::string > getRow(size_t index) const
virtual const T * getElement() const
Exception thrown when a given row name is not found is a DataTable object.
std::vector< std::string > & getColumn(size_t index)
std::string getColumnName(size_t index) const
Get a given column name.
size_t getNumberOfRows() const
static bool isEmpty(const std::string &s)
Tell if a string is empty.
Excpetion thrown when attempting to duplicate a column name.
Exception thrown when trying to retrieve a column by its name and no column names have been specified...
bool hasRow(const std::string &rowName) const
Tell is a given row exists.
std::string & operator()(size_t rowIndex, size_t colIndex)
std::vector< std::string > * colNames_
void addRow(const std::vector< std::string > &newRow)
Add a new row.
const std::deque< std::string > & getTokens() const
Retrieve all tokens.
virtual OutputStream & endLine()=0
bool hasColumnNames() const
DataTable(size_t nRow, size_t nCol)
Build a new void DataTable object with nRow rows and nCol columns.
bool hasColumn(const std::string &colName) const
Tell is a given column exists.
void deleteColumn(size_t index)
Delete the given column.
static DataTable * read(std::istream &in, const std::string &sep="\, bool header=true, int rowNames=-1)
Read a table form a stream in CSV-like format.
Index out of bounds exception class.
Exception thrown when a given name is not found is a DataTable object.
void deleteRow(size_t index)
Delete the given row.
Exception thrown when a dimension problem occured.
General exception class dealing with column names.
General exception class dealing with row names.
size_t getNumberOfColumns() const
DataTable & operator=(const DataTable &table)
static void write(const DataTable &data, std::ostream &out, const std::string &sep="\, bool alignHeaders=false)
Write a DataTable object to stream in CVS-like format.
std::vector< std::string > getRowNames() const
Get the row names of this table.