本文整理汇总了C++中DataFrame::attr方法的典型用法代码示例。如果您正苦于以下问题:C++ DataFrame::attr方法的具体用法?C++ DataFrame::attr怎么用?C++ DataFrame::attr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataFrame
的用法示例。
在下文中一共展示了DataFrame::attr方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: grouped_subset
inline DataFrame grouped_subset( const Data& gdf, const LogicalVector& test, const CharacterVector& names, CharacterVector classes){
DataFrame data = gdf.data() ;
DataFrame res = subset( data, test, names, classes) ;
res.attr("vars") = data.attr("vars") ;
strip_index(res);
return Data(res).data() ;
}
示例2: 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;
}
示例3: arrange_impl
// [[Rcpp::export]]
List arrange_impl( DataFrame data, LazyDots dots ){
if( data.size() == 0 ) return data ;
check_valid_colnames(data) ;
assert_all_white_list(data) ;
if( dots.size() == 0 || data.nrows() == 0) return data ;
int nargs = dots.size() ;
List variables(nargs) ;
LogicalVector ascending(nargs) ;
for(int i=0; i<nargs; i++){
const Lazy& lazy = dots[i] ;
Shield<SEXP> call_( lazy.expr() ) ;
SEXP call = call_ ;
bool is_desc = TYPEOF(call) == LANGSXP && Rf_install("desc") == CAR(call) ;
CallProxy call_proxy(is_desc ? CADR(call) : call, data, lazy.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, data.attr("class") ) ;
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 ;
res.attr( "vars" ) = data.attr("vars" ) ;
return GroupedDataFrame(res).data() ;
}
SET_ATTRIB(res, strip_group_attributes(res));
return res ;
}
示例4: structure_mutate
SEXP structure_mutate( const NamedListAccumulator<SEXP>& accumulator, const DataFrame& df, CharacterVector classes){
List res = accumulator ;
res.attr("class") = classes ;
set_rownames( res, df.nrows() ) ;
res.attr( "vars") = df.attr("vars") ;
res.attr( "labels" ) = df.attr("labels" );
res.attr( "index") = df.attr("index") ;
res.attr( "indices" ) = df.attr("indices" ) ;
return res ;
}
示例5: filter_grouped_single_env
DataFrame filter_grouped_single_env( const GroupedDataFrame& gdf, const List& args, const Environment& env){
const DataFrame& data = gdf.data() ;
CharacterVector names = data.names() ;
SymbolSet set ;
for( int i=0; i<names.size(); i++){
set.insert( Rf_install( names[i] ) ) ;
}
// a, b, c -> a & b & c
Call call( and_calls( args, set ) ) ;
int nrows = data.nrows() ;
LogicalVector test = no_init(nrows);
LogicalVector g_test ;
GroupedCallProxy call_proxy( call, gdf, env ) ;
int ngroups = gdf.ngroups() ;
GroupedDataFrame::group_iterator git = gdf.group_begin() ;
for( int i=0; i<ngroups; i++, ++git){
SlicingIndex indices = *git ;
int chunk_size = indices.size() ;
g_test = call_proxy.get( indices );
check_filter_result(g_test, chunk_size ) ;
for( int j=0; j<chunk_size; j++){
test[ indices[j] ] = g_test[j] ;
}
}
DataFrame res = subset( data, test, names, classes_grouped() ) ;
res.attr( "vars") = data.attr("vars") ;
return res ;
}
示例6: filter_grouped_multiple_env
// version of grouped filter when contributions to ... come from several environment
DataFrame filter_grouped_multiple_env( const GroupedDataFrame& gdf, const List& args, const DataDots& dots){
const DataFrame& data = gdf.data() ;
CharacterVector names = data.names() ;
SymbolSet set ;
for( int i=0; i<names.size(); i++){
set.insert( Rf_install( names[i] ) ) ;
}
int nrows = data.nrows() ;
LogicalVector test(nrows, TRUE);
LogicalVector g_test ;
for( int k=0; k<args.size(); k++){
Call call( (SEXP)args[k] ) ;
GroupedCallProxy call_proxy( call, gdf, dots.envir(k) ) ;
int ngroups = gdf.ngroups() ;
GroupedDataFrame::group_iterator git = gdf.group_begin() ;
for( int i=0; i<ngroups; i++, ++git){
SlicingIndex indices = *git ;
int chunk_size = indices.size() ;
g_test = call_proxy.get( indices );
check_filter_result(g_test, chunk_size ) ;
for( int j=0; j<chunk_size; j++){
test[ indices[j] ] = test[ indices[j] ] & g_test[j] ;
}
}
}
DataFrame res = subset( data, test, names, classes_grouped() ) ;
res.attr( "vars") = data.attr("vars") ;
return res ;
}
示例7: subset
DataFrame subset( DataFrame x, DataFrame y, const Index& indices_x, const Index& indices_y, CharacterVector by, CharacterVector classes ){
CharacterVector x_columns = x.names() ;
DataFrameVisitors visitors_x(x, x_columns) ;
CharacterVector all_y_columns = y.names() ;
CharacterVector y_columns = setdiff( all_y_columns, by ) ;
JoinColumnSuffixer suffixer(x_columns, y_columns, by) ;
DataFrameVisitors visitors_y(y, y_columns) ;
int nrows = indices_x.size() ;
int nv_x = visitors_x.size(), nv_y = visitors_y.size() ;
List out(nv_x+nv_y);
CharacterVector names(nv_x+nv_y) ;
int k=0;
for( ; k<nv_x; k++){
out[k] = visitors_x.get(k)->subset(indices_x) ;
names[k] = suffixer.get( x_columns[k], ".x" ) ;
}
for( int i=0; i<nv_y; i++, k++){
out[k] = visitors_y.get(i)->subset(indices_y) ;
names[k] = suffixer.get(y_columns[i], ".y" ) ;
}
out.attr("class") = classes ;
set_rownames(out, nrows) ;
out.names() = names ;
SEXP vars = x.attr( "vars" ) ;
if( !Rf_isNull(vars) )
out.attr( "vars" ) = vars ;
return (SEXP)out ;
}
示例8: as_regular_df
// [[Rcpp::export]]
DataFrame as_regular_df(DataFrame df){
DataFrame copy = shallow_copy(df) ;
SET_ATTRIB(copy, strip_group_attributes(df)) ;
SET_OBJECT(copy, OBJECT(df)) ;
copy.attr("class") = CharacterVector::create("data.frame") ;
return copy ;
}
示例9: 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" ) ) ;
}
示例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: 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") ) ;
}
示例12: distinct_impl
// [[Rcpp::export]]
SEXP distinct_impl(DataFrame df, CharacterVector vars, CharacterVector keep) {
if (df.size() == 0)
return df;
// No vars means ungrouped data with keep_all = TRUE.
if (vars.size() == 0)
return df;
check_valid_colnames(df);
if (!vars.size()) {
vars = df.names();
}
DataFrameVisitors visitors(df, vars);
std::vector<int> indices;
VisitorSetIndexSet<DataFrameVisitors> set(visitors);
int n = df.nrows();
for (int i=0; i<n; i++) {
if (set.insert(i).second) {
indices.push_back(i);
}
}
return DataFrameSubsetVisitors(df, keep).subset(indices, df.attr("class"));
}
示例13: filter_grouped
DataFrame filter_grouped( const GroupedDataFrame& gdf, List args, Environment env){
// a, b, c -> a & b & c
Language call = and_calls( args ) ;
const DataFrame& data = gdf.data() ;
int nrows = data.nrows() ;
LogicalVector test = no_init(nrows);
LogicalVector g_test ;
GroupedCallProxy call_proxy( call, gdf, env ) ;
int ngroups = gdf.ngroups() ;
GroupedDataFrame::group_iterator git = gdf.group_begin() ;
for( int i=0; i<ngroups; i++, ++git){
SlicingIndex indices = *git ;
g_test = call_proxy.get( indices );
int chunk_size = indices.size() ;
for( int j=0; j<chunk_size; j++){
test[ indices[j] ] = g_test[j] ;
}
}
DataFrame res = subset( data, test, data.names(), classes_grouped() ) ;
res.attr( "vars") = data.attr("vars") ;
return res ;
}
示例14: build_index_cpp
// [[Rcpp::export]]
DataFrame build_index_cpp( DataFrame data ){
CharacterVector vars = Rf_getAttrib( data.attr( "vars" ), R_NamesSymbol ) ;
DataFrameVisitors visitors(data, vars) ;
ChunkIndexMap map( visitors ) ;
train_push_back( map, data.nrows() ) ;
DataFrame labels = visitors.subset( map, "data.frame") ;
int ngroups = labels.nrows() ;
OrderVisitors order_labels( labels, vars ) ;
IntegerVector orders = order_labels.apply() ;
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 ;
}
IntegerVector group_sizes = no_init( ngroups );
int biggest_group = 0 ;
std::vector<int> indices ;
indices.reserve( data.nrows() );
for( int i=0; i<ngroups; i++){
const std::vector<int>& chunk = *chunks[orders[i]] ;
push_back( indices, chunk ) ;
biggest_group = std::max( biggest_group, (int)chunk.size() );
group_sizes[i] = chunk.size() ;
}
DataFrameVisitors all_variables_visitors(data, data.names() ) ;
data = all_variables_visitors.subset( indices, classes_grouped() ) ;
// TODO: we own labels, so perhaps we can do an inplace sort,
// to reuse its memory instead of creating a new data frame
DataFrameVisitors labels_visitors( labels, vars) ;
labels = labels_visitors.subset( orders, "data.frame" ) ;
labels.attr( "vars" ) = R_NilValue ;
data.attr( "group_sizes") = group_sizes ;
data.attr( "biggest_group_size" ) = biggest_group ;
data.attr( "labels" ) = labels ;
return data ;
}
示例15: mutate_grouped
SEXP mutate_grouped(GroupedDataFrame gdf, List args, Environment env){
const DataFrame& df = gdf.data() ;
int nexpr = args.size() ;
CharacterVector results_names = args.names() ;
GroupedCallProxy proxy(gdf, env) ;
Shelter<SEXP> __ ;
for( int i=0; i<nexpr; i++){
proxy.set_call( args[i] );
boost::scoped_ptr<Gatherer> gather( gatherer( proxy, gdf ) );
proxy.input( results_names[i], __( gather->collect() ) ) ;
}
DataFrame res = structure_mutate( proxy, df, results_names, classes_grouped() ) ;
res.attr( "vars") = df.attr("vars") ;
res.attr( "labels" ) = df.attr("labels" );
res.attr( "index") = df.attr("index") ;
return res ;
}