STK++ 0.9.13
STK_DataFrameToArray2D.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23 */
24
25/*
26 * Project: stkpp::DManager
27 * created on: 9 juin 2011
28 * Purpose: Create an utility class in order to transfer the Data from
29 * a DataFrame in an Array2D.
30 * Author: iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
31 *
32 **/
33
38#ifndef STK_DATAFRAMETOARRAY2D_H
39#define STK_DATAFRAMETOARRAY2D_H
40
42
43namespace STK
44{
45
54template<class Type>
56{
57 public:
61 DataFrameToArray2D( DataFrame const& df): df_(df), p_data_(0) {}
65 void run()
66 {
67 p_data_ = new Array2D<Type>();
68 // for each field Try a type conversion
69 for(int iVar = df_.beginCols(); iVar<=df_.lastIdxCols(); iVar++)
70 {
71 IVariable* const p_var = df_.elt(iVar);
72 // if there is a variable
73 if (p_var)
74 {
75 // check the type of the variable
76 if (p_var->getType() == IdTypeImpl<Type>::returnType())
77 {
79 p_data_->pushBackCols(*p_variable);
80 }
81 }
82 }
83 }
88 inline Array2D<Type>* p_data() { return p_data_;}
90 inline void release() { p_data_ =0;}
96 {
97 // check if there exists data
98 if (!p_data_) return;
99 // get the first index
100 const int firstCol = p_data_->beginCols(), firstRow = p_data_->beginRows();
101 // get the last index
102 int lastCol = p_data_->lastIdxCols(), lastRow = p_data_->lastIdxRows();
103 if (byRow)
104 {
105 // loop on the rows
106 for (int i= firstRow; i <= lastRow; ++i)
107 {
108 bool asNA = false;
109 // loop on the element of the row
110 for (int j= firstCol; j <= lastCol; ++j)
111 {
113 {
114 asNA = true;
115 break;
116 }
117 }
118 // remove current row
119 if (asNA)
120 {
121 p_data_->eraseRows(i);
122 i--;
123 lastRow--;
124 }
125 }
126 }
127 else
128 {
129 // loop on the column
130 for (int j= firstCol; j <= lastCol; ++j)
131 {
132 bool asNA = false;
133 // loop on the element of the column
134 for (int i= firstRow; i <= lastRow; ++i)
135 {
137 {
138 asNA = true;
139 break;
140 }
141 }
142 // remove current column
143 if (asNA)
144 {
145 p_data_->eraseCols(j);
146 j--;
147 lastCol--;
148 }
149 }
150 }
151 }
152
153 private:
158};
159
160} // namespace STK
161
162#endif /* STK_DATAFRAMETOARRAY2D_H */
In this file, we define the final class Array2D.
The DataFrameToArray2D class allow to export the data of some TYPE stored in a DataFrame to be export...
void eraseNAValues(bool byRow)
remove NA values from the Array2D
DataFrameToArray2D(DataFrame const &df)
Constructor.
Array2D< Type > * p_data_
A pointer on the 2D Container the class will create.
DataFrame const & df_
A reference to the DataFrame we want to exprt.
void release()
release the Array2D.
Array2D< Type > * p_data()
Accessor.
DataFrame is a List of Variable with the same number of rows.
int beginCols() const
IVariable *& elt(int const &i)
access to an element.
int lastIdxCols() const
IVariable is an Interface class for all Variables classes.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
The namespace STK is the main domain space of the Statistical ToolKit project.
Arithmetic properties of STK fundamental types.
static Base::IdType returnType()