本文整理汇总了C++中DataFrame::nrows方法的典型用法代码示例。如果您正苦于以下问题:C++ DataFrame::nrows方法的具体用法?C++ DataFrame::nrows怎么用?C++ DataFrame::nrows使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataFrame
的用法示例。
在下文中一共展示了DataFrame::nrows方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: inner_join_impl
// [[Rcpp::export]]
DataFrame inner_join_impl(DataFrame x, DataFrame y,
CharacterVector by_x, CharacterVector by_y,
std::string& suffix_x, std::string& suffix_y,
bool na_match) {
if (by_x.size() == 0) stop("no variable to join by");
typedef VisitorSetIndexMap<DataFrameJoinVisitors, std::vector<int> > Map;
DataFrameJoinVisitors visitors(x, y, SymbolVector(by_x), SymbolVector(by_y), true, na_match);
Map map(visitors);
int n_x = x.nrows(), n_y = y.nrows();
std::vector<int> indices_x;
std::vector<int> indices_y;
train_push_back_right(map, n_y);
for (int i = 0; i < n_x; i++) {
Map::iterator it = map.find(i);
if (it != map.end()) {
push_back_right(indices_y, it->second);
push_back(indices_x, i, it->second.size());
}
}
return subset_join(x, y,
indices_x, indices_y,
by_x, by_y,
suffix_x, suffix_y,
get_class(x)
);
}
示例2: semi_join_impl
// [[Rcpp::export]]
DataFrame semi_join_impl(DataFrame x, DataFrame y, CharacterVector by_x, CharacterVector by_y, bool na_match) {
if (by_x.size() == 0) stop("no variable to join by");
typedef VisitorSetIndexMap<DataFrameJoinVisitors, std::vector<int> > Map;
DataFrameJoinVisitors visitors(x, y, SymbolVector(by_x), SymbolVector(by_y), false, na_match);
Map map(visitors);
// train the map in terms of x
train_push_back(map, x.nrows());
int n_y = y.nrows();
// this will collect indices from rows in x that match rows in y
std::vector<int> indices;
for (int i = 0; i < n_y; i++) {
// find a row in x that matches row i from y
Map::iterator it = map.find(-i - 1);
if (it != map.end()) {
// collect the indices and remove them from the
// map so that they are only found once.
push_back(indices, it->second);
map.erase(it);
}
}
const DataFrame& out = subset(x, indices, x.names(), get_class(x));
strip_index(out);
return out;
}
示例3: anti_join_impl
// [[Rcpp::export]]
DataFrame anti_join_impl(DataFrame x, DataFrame y, CharacterVector by_x, CharacterVector by_y, bool na_match) {
if (by_x.size() == 0) stop("no variable to join by");
typedef VisitorSetIndexMap<DataFrameJoinVisitors, std::vector<int> > Map;
DataFrameJoinVisitors visitors(x, y, SymbolVector(by_x), SymbolVector(by_y), false, na_match);
Map map(visitors);
// train the map in terms of x
train_push_back(map, x.nrows());
int n_y = y.nrows();
// remove the rows in x that match
for (int i = 0; i < n_y; i++) {
Map::iterator it = map.find(-i - 1);
if (it != map.end())
map.erase(it);
}
// collect what's left
std::vector<int> indices;
for (Map::iterator it = map.begin(); it != map.end(); ++it)
push_back(indices, it->second);
const DataFrame& out = subset(x, indices, x.names(), get_class(x));
strip_index(out);
return out;
}
示例4: filter_not_grouped
SEXP filter_not_grouped( DataFrame df, List args, const DataDots& dots){
CharacterVector names = df.names() ;
SymbolSet set ;
for( int i=0; i<names.size(); i++){
set.insert( Rf_install( names[i] ) ) ;
}
if( dots.single_env() ){
Environment env = dots.envir(0) ;
// a, b, c -> a & b & c
Shield<SEXP> call( and_calls( args, set ) ) ;
// replace the symbols that are in the data frame by vectors from the data frame
// and evaluate the expression
CallProxy proxy( (SEXP)call, df, env ) ;
LogicalVector test = proxy.eval() ;
check_filter_result(test, df.nrows());
DataFrame res = subset( df, test, df.names(), classes_not_grouped() ) ;
return res ;
} else {
int nargs = args.size() ;
CallProxy first_proxy(args[0], df, dots.envir(0) ) ;
LogicalVector test = first_proxy.eval() ;
check_filter_result(test, df.nrows());
for( int i=1; i<nargs; i++){
LogicalVector test2 = CallProxy(args[i], df, dots.envir(i) ).eval() ;
combine_and(test, test2) ;
}
DataFrame res = subset( df, test, df.names(), classes_not_grouped() ) ;
return res ;
}
}
示例5: semi_join_impl
// [[Rcpp::export]]
DataFrame semi_join_impl( DataFrame x, DataFrame y, CharacterVector by){
typedef VisitorSetIndexMap<DataFrameJoinVisitors, std::vector<int> > Map ;
DataFrameJoinVisitors visitors(x, y, by) ;
Map map(visitors);
// train the map in terms of x
train_push_back( map, x.nrows(), x.nrows() / 10) ;
int n_y = y.nrows() ;
// this will collect indices from rows in x that match rows in y
std::vector<int> indices ;
for( int i=0; i<n_y; i++){
// find a row in x that matches row i from y
Map::iterator it = map.find(-i-1) ;
if( it != map.end() ){
// collect the indices and remove them from the
// map so that they are only found once.
push_back( indices, it->second ) ;
map.erase(it) ;
}
}
return subset(x, indices, x.names(), x.attr("class") ) ;
}
示例6: right_join_impl
// [[Rcpp::export]]
DataFrame right_join_impl( DataFrame x, DataFrame y, CharacterVector by){
typedef VisitorSetIndexMap<DataFrameJoinVisitors, std::vector<int> > Map ;
DataFrameJoinVisitors visitors(x, y, by) ;
Map map(visitors);
// train the map in terms of y
train_push_back( map, x.nrows(), x.nrows() / 10 ) ;
std::vector<int> indices_x ;
std::vector<int> indices_y ;
int n_y = y.nrows() ;
for( int i=0; i<n_y; i++){
// find a row in y that matches row i in x
Map::iterator it = map.find(-i-1) ;
if( it != map.end() ){
push_back( indices_x, it->second ) ;
push_back( indices_y, i, it->second.size() ) ;
} else {
indices_x.push_back(-1) ; // mark NA
indices_y.push_back(i) ;
}
}
return subset( x, y, indices_x, indices_y, by, x.attr( "class" ) ) ;
}
示例7: right_join_impl
// [[Rcpp::export]]
DataFrame right_join_impl(DataFrame x, DataFrame y,
IntegerVector by_x, IntegerVector by_y,
IntegerVector aux_x, IntegerVector aux_y,
bool na_match) {
check_by(by_x);
typedef VisitorSetIndexMap<DataFrameJoinVisitors, std::vector<int> > Map;
DataFrameJoinVisitors visitors(x, y, by_x, by_y, false, na_match);
Map map(visitors);
// train the map in terms of x
train_push_back(map, x.nrows());
std::vector<int> indices_x;
std::vector<int> indices_y;
int n_y = y.nrows();
for (int i = 0; i < n_y; i++) {
// find a row in y that matches row i in x
Map::iterator it = map.find(-i - 1);
if (it != map.end()) {
push_back(indices_x, it->second);
push_back(indices_y, i, it->second.size());
} else {
indices_x.push_back(-i - 1); // point to the i-th row in the right table
indices_y.push_back(i);
}
}
return subset_join(x, y,
indices_x, indices_y,
by_x, by_y,
aux_x, aux_y,
get_class(x)
);
}
示例8: inner_join_impl
// [[Rcpp::export]]
DataFrame inner_join_impl(DataFrame x, DataFrame y,
IntegerVector by_x, IntegerVector by_y,
IntegerVector aux_x, IntegerVector aux_y,
bool na_match) {
check_by(by_x);
typedef VisitorSetIndexMap<DataFrameJoinVisitors, std::vector<int> > Map;
DataFrameJoinVisitors visitors(x, y, by_x, by_y, false, na_match);
Map map(visitors);
int n_x = x.nrows(), n_y = y.nrows();
std::vector<int> indices_x;
std::vector<int> indices_y;
train_push_back_right(map, n_y);
for (int i = 0; i < n_x; i++) {
Map::iterator it = map.find(i);
if (it != map.end()) {
push_back_right(indices_y, it->second);
push_back(indices_x, i, it->second.size());
}
}
return subset_join(x, y,
indices_x, indices_y,
by_x, by_y,
aux_x, aux_y,
get_class(x)
);
}
示例9: right_join_impl
// [[Rcpp::export]]
DataFrame right_join_impl(DataFrame x, DataFrame y,
CharacterVector by_x, CharacterVector by_y,
std::string& suffix_x, std::string& suffix_y,
bool na_match) {
if (by_x.size() == 0) stop("no variable to join by");
typedef VisitorSetIndexMap<DataFrameJoinVisitors, std::vector<int> > Map;
DataFrameJoinVisitors visitors(x, y, SymbolVector(by_x), SymbolVector(by_y), true, na_match);
Map map(visitors);
// train the map in terms of x
train_push_back(map, x.nrows());
std::vector<int> indices_x;
std::vector<int> indices_y;
int n_y = y.nrows();
for (int i = 0; i < n_y; i++) {
// find a row in y that matches row i in x
Map::iterator it = map.find(-i - 1);
if (it != map.end()) {
push_back(indices_x, it->second);
push_back(indices_y, i, it->second.size());
} else {
indices_x.push_back(-i - 1); // point to the i-th row in the right table
indices_y.push_back(i);
}
}
return subset_join(x, y,
indices_x, indices_y,
by_x, by_y,
suffix_x, suffix_y,
get_class(x)
);
}
示例10: arrange_impl
// [[Rcpp::export]]
DataFrame arrange_impl( DataFrame data, List args, DataDots dots ){
int nargs = args.size() ;
List variables(nargs) ;
LogicalVector ascending(nargs) ;
Shelter<SEXP> __ ;
for(int i=0; i<nargs; i++){
SEXP call = args[i] ;
bool is_desc = TYPEOF(call) == LANGSXP && Rf_install("desc") == CAR(call) ;
CallProxy call_proxy( is_desc ? CADR(call) : call, data, dots.envir(i)) ;
variables[i] = __(call_proxy.eval()) ;
if( Rf_length(variables[i]) != data.nrows() ){
std::stringstream s ;
s << "incorrect size ("
<< Rf_length(variables[i])
<< "), expecting :"
<< data.nrows() ;
stop(s.str()) ;
}
ascending[i] = !is_desc ;
}
OrderVisitors o(variables,ascending, nargs) ;
IntegerVector index = o.apply() ;
DataFrameVisitors visitors( data, data.names() ) ;
DataFrame res = visitors.subset(index, data.attr("class") ) ;
return res;
}
示例11: build_index_cpp
DataFrame build_index_cpp(DataFrame data) {
SymbolVector vars(get_vars(data));
const int nvars = vars.size();
CharacterVector names = data.names();
IntegerVector indx = vars.match_in_table(names);
for (int i = 0; i < nvars; ++i) {
int pos = indx[i];
if (pos == NA_INTEGER) {
stop("unknown column '%s' ", vars[i].get_utf8_cstring());
}
SEXP v = data[pos - 1];
if (!white_list(v) || TYPEOF(v) == VECSXP) {
stop(
"cannot group column %s, of class '%s'",
vars[i].get_utf8_cstring(),
get_single_class(v));
}
}
DataFrameVisitors visitors(data, vars);
ChunkIndexMap map(visitors);
train_push_back(map, data.nrows());
DataFrame labels = DataFrameSubsetVisitors(data, vars).subset(map, "data.frame");
int ngroups = labels.nrows();
IntegerVector labels_order = OrderVisitors(labels).apply();
labels = DataFrameSubsetVisitors(labels).subset(labels_order, "data.frame");
List indices(ngroups);
IntegerVector group_sizes = no_init(ngroups);
int biggest_group = 0;
ChunkIndexMap::const_iterator it = map.begin();
std::vector<const std::vector<int>* > chunks(ngroups);
for (int i = 0; i < ngroups; i++, ++it) {
chunks[i] = &it->second;
}
for (int i = 0; i < ngroups; i++) {
int idx = labels_order[i];
const std::vector<int>& chunk = *chunks[idx];
indices[i] = chunk;
group_sizes[i] = chunk.size();
biggest_group = std::max(biggest_group, (int)chunk.size());
}
data.attr("indices") = indices;
data.attr("group_sizes") = group_sizes;
data.attr("biggest_group_size") = biggest_group;
data.attr("labels") = labels;
set_class(data, CharacterVector::create("grouped_df", "tbl_df", "tbl", "data.frame"));
return data;
}
示例12: arrange_impl
// [[Rcpp::export]]
List arrange_impl(DataFrame data, QuosureList quosures) {
if (data.size() == 0) return data;
check_valid_colnames(data);
assert_all_white_list(data);
if (quosures.size() == 0 || data.nrows() == 0) return data;
int nargs = quosures.size();
List variables(nargs);
LogicalVector ascending(nargs);
for (int i = 0; i < nargs; i++) {
const NamedQuosure& quosure = quosures[i];
Shield<SEXP> call_(quosure.expr());
SEXP call = call_;
bool is_desc = TYPEOF(call) == LANGSXP && Rf_install("desc") == CAR(call);
CallProxy call_proxy(is_desc ? CADR(call) : call, data, quosure.env());
Shield<SEXP> v(call_proxy.eval());
if (!white_list(v)) {
stop("cannot arrange column of class '%s'", get_single_class(v));
}
if (Rf_inherits(v, "data.frame")) {
DataFrame df(v);
int nr = df.nrows();
if (nr != data.nrows()) {
stop("data frame column with incompatible number of rows (%d), expecting : %d", nr, data.nrows());
}
} else if (Rf_isMatrix(v)) {
stop("can't arrange by a matrix");
} else {
if (Rf_length(v) != data.nrows()) {
stop("incorrect size (%d), expecting : %d", Rf_length(v), data.nrows());
}
}
variables[i] = v;
ascending[i] = !is_desc;
}
OrderVisitors o(variables, ascending, nargs);
IntegerVector index = o.apply();
DataFrameSubsetVisitors visitors(data, data.names());
List res = visitors.subset(index, get_class(data));
if (is<GroupedDataFrame>(data)) {
// so that all attributes are recalculated (indices ... )
// see the lazyness feature in GroupedDataFrame
// if we don't do that, we get the values of the un-arranged data
// set for free from subset (#1064)
res.attr("labels") = R_NilValue;
copy_vars(res, data);
return GroupedDataFrame(res).data();
}
SET_ATTRIB(res, strip_group_attributes(res));
return res;
}
示例13: filter_not_grouped
DataFrame filter_not_grouped( DataFrame df, const LazyDots& dots){
CharacterVector names = df.names() ;
SymbolSet set ;
for( int i=0; i<names.size(); i++){
set.insert( Rf_installChar( names[i] ) ) ;
}
if( dots.single_env() ){
Environment env = dots[0].env() ;
// a, b, c -> a & b & c
Shield<SEXP> call( and_calls( dots, set, env ) ) ;
// replace the symbols that are in the data frame by vectors from the data frame
// and evaluate the expression
CallProxy proxy( (SEXP)call, df, env ) ;
LogicalVector test = check_filter_logical_result(proxy.eval()) ;
if( test.size() == 1){
if( test[0] == TRUE ){
return df ;
} else {
return empty_subset(df, df.names(), classes_not_grouped()) ;
}
} else {
check_filter_result(test, df.nrows());
return subset(df, test, classes_not_grouped() ) ;
}
} else {
int nargs = dots.size() ;
Call call(dots[0].expr());
CallProxy first_proxy(call, df, dots[0].env() ) ;
LogicalVector test = check_filter_logical_result(first_proxy.eval()) ;
if( test.size() == 1 ) {
if( !test[0] ){
return empty_subset(df, df.names(), classes_not_grouped() ) ;
}
} else {
check_filter_result(test, df.nrows());
}
for( int i=1; i<nargs; i++){
Rcpp::checkUserInterrupt() ;
Call call( dots[i].expr() ) ;
CallProxy proxy(call, df, dots[i].env() ) ;
LogicalVector test2 = check_filter_logical_result(proxy.eval()) ;
if( combine_and(test, test2) ){
return empty_subset(df, df.names(), classes_not_grouped() ) ;
}
}
DataFrame res = subset( df, test, classes_not_grouped() ) ;
return res ;
}
}
示例14: grouped_indices_impl
// [[Rcpp::export]]
IntegerVector grouped_indices_impl(DataFrame data, ListOf<Symbol> symbols) {
int nsymbols = symbols.size();
if (nsymbols == 0)
return rep(1, data.nrows());
CharacterVector vars(nsymbols);
for (int i=0; i<nsymbols; i++) {
vars[i] = PRINTNAME(symbols[i]);
const char* name = vars[i];
SEXP v;
try {
v = data[name];
} catch (...) {
stop("unknown column '%s'", name);
}
if (!white_list(v) || TYPEOF(v) == VECSXP) {
stop("cannot group column %s, of class '%s'", name, get_single_class(v));
}
}
DataFrameVisitors visitors(data, vars);
ChunkIndexMap map(visitors);
int n = data.nrows();
train_push_back(map, n);
DataFrame labels = DataFrameSubsetVisitors(data, vars).subset(map, "data.frame");
IntegerVector labels_order = OrderVisitors(labels).apply();
labels = DataFrameSubsetVisitors(labels).subset(labels_order, "data.frame");
int ngroups = map.size();
IntegerVector res = no_init(n);
std::vector<const std::vector<int>* > chunks(ngroups);
ChunkIndexMap::const_iterator it = map.begin();
for (int i=0; i<ngroups; i++, ++it) {
chunks[i] = &it->second;
}
for (int i=0; i<ngroups; i++) {
int idx = labels_order[i];
const std::vector<int>& v = *chunks[idx];
int n_index = v.size();
for (int j=0; j<n_index; j++) {
res[ v[j] ] = i+1;
}
}
return res;
}
示例15: full_join_impl
// [[Rcpp::export]]
DataFrame full_join_impl(DataFrame x, DataFrame y,
IntegerVector by_x, IntegerVector by_y,
IntegerVector aux_x, IntegerVector aux_y,
bool na_match) {
check_by(by_x);
typedef VisitorSetIndexMap<DataFrameJoinVisitors, std::vector<int> > Map;
DataFrameJoinVisitors visitors(y, x, by_y, by_x, false, na_match);
Map map(visitors);
// train the map in terms of y
train_push_back(map, y.nrows());
std::vector<int> indices_x;
std::vector<int> indices_y;
int n_x = x.nrows(), n_y = y.nrows();
// get both the matches and the rows from left but not right
for (int i = 0; i < n_x; i++) {
// find a row in y that matches row i in x
Map::iterator it = map.find(-i - 1);
if (it != map.end()) {
push_back(indices_y, it->second);
push_back(indices_x, i, it->second.size());
} else {
indices_y.push_back(-1); // mark NA
indices_x.push_back(i);
}
}
// train a new map in terms of x this time
DataFrameJoinVisitors visitors2(x, y, by_x, by_y, false, na_match);
Map map2(visitors2);
train_push_back(map2, x.nrows());
for (int i = 0; i < n_y; i++) {
// try to find row in x that matches this row of y
Map::iterator it = map2.find(-i - 1);
if (it == map2.end()) {
indices_x.push_back(-i - 1);
indices_y.push_back(i);
}
}
return subset_join(x, y,
indices_x, indices_y,
by_x, by_y,
aux_x, aux_y,
get_class(x)
);
}