Back to Documentations

Signature Description Parameters
#include <DataFrame/DataFrameStatsVisitors.h>

template<typename T, typename I = unsigned long,
         std::size_t A = 0>
struct FactorizeVisitor;
This is a "single action visitor", meaning it is passed the whole data vector in one call and you must use the single_act_visit() interface.

This functor produces a Boolean vector (an actual vector of chars, because bool vector in C++ is broken) of same length as the given column. Each element in the result vector is true if the corresponding element in the column passes the factorization function, otherwise it is false.
The constructor takes one parameter which is the factorization function
    using factor_func = std::function<char(const T &val)>;

    explicit FactorizeVisitor(factor_func f);
            
The result is a vector of char
T: Column data type.
I: Index type.
A: Memory alignment boundary for vectors. Default is system default alignment
static void test_FactorizeVisitor()  {

    std::cout << "\nTesting FactorizeVisitor{ } ..." << std::endl;

    MyDataFrame df;

    StlVecType<unsigned long>  idxvec = { 1UL, 2UL, 3UL, 10UL, 5UL, 7UL, 8UL, 12UL, 9UL, 12UL, 10UL, 13UL, 10UL, 15UL, 14UL };
    StlVecType<double>         dblvec = { 0.0, 15.0, 14.0, 2.0, 1.0, 12.0, 11.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 9.0, 10.0 };
    StlVecType<double>         dblvec2 = { 100.0, 101.0, 102.0, 103.0, 104.0, 105.0, 106.55, 107.34, 1.8, 111.0, 112.0, 113.0, 114.0, 115.0, 116.0 };
    StlVecType<int>            intvec = { 1, 2, 3, 4, 5, 8, 6, 7, 11, 14, 9 };
    StlVecType<std::string>    strvec = { "zz", "bb", "cc", "ww", "ee", "ff", "gg", "hh", "ii", "jj", "kk", "ll", "mm", "nn", "oo" };

    df.load_data(std::move(idxvec),
                 std::make_pair("dbl_col", dblvec),
                 std::make_pair("dbl_col_2", dblvec2),
                 std::make_pair("str_col", strvec));
    df.load_column("int_col", std::move(intvec), nan_policy::dont_pad_with_nans);

    FactorizeVisitor<double, unsigned long, 64>  fact([] (const double &f) -> char {
                                                          return (char(f > 106.0 && f < 114.0));
                                                      });

    df.load_column("bool_col", df.single_act_visit<double>("dbl_col_2", fact).get_result());
    assert(df.get_column<char>("bool_col").size() == 15);
    assert(df.get_column<char>("bool_col")[0] == false);
    assert(df.get_column<char>("bool_col")[4] == false);
    assert(df.get_column<char>("bool_col")[6] == true);
    assert(df.get_column<char>("bool_col")[7] == true);
    assert(df.get_column<char>("bool_col")[8] == false);
    assert(df.get_column<char>("bool_col")[9] == true);
    assert(df.get_column<char>("bool_col")[11] == true);
    assert(df.get_column<char>("bool_col")[13] == false);
}

C++ DataFrame