本文整理汇总了C++中CharacterVector::size方法的典型用法代码示例。如果您正苦于以下问题:C++ CharacterVector::size方法的具体用法?C++ CharacterVector::size怎么用?C++ CharacterVector::size使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CharacterVector
的用法示例。
在下文中一共展示了CharacterVector::size方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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"));
}
示例2: node_attr
// [[Rcpp::export]]
SEXP node_attr(XPtrNode node, std::string name, CharacterVector missing,
CharacterVector nsMap) {
if (missing.size() != 1)
Rcpp::stop("`missing` should be length 1");
SEXP missingVal = missing[0];
xmlChar* string;
if (nsMap.size() == 0) {
string = xmlGetProp(node.get(), asXmlChar(name));
} else {
size_t colon = name.find(":");
if (colon == std::string::npos) {
// Has namespace spec, but attribute not qualified, so look for attribute
// without namespace
string = xmlGetNoNsProp(node.get(), asXmlChar(name));
} else {
// Split name into prefix & attr, then look up full url
std::string
prefix = name.substr(0, colon),
attr = name.substr(colon + 1, name.size() - 1);
std::string url = NsMap(nsMap).findUrl(prefix);
string = xmlGetNsProp(node.get(), asXmlChar(attr), asXmlChar(url));
}
}
return CharacterVector(Xml2String(string).asRString(missingVal));
}
示例3: CSvmClassifier
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
NumericVector CSvmClassifier(const Function& segmentCN, const CharacterVector& traindoc,
const NumericVector& type, const CharacterVector& testdoc,
double c_contr, int iterate=10000L, double c_epsilon=1.0e-2){
Rcout <<"CSvmClassifier begin..."<< std::endl;
if(unique(type).size() != 2){
stop("SVMClassifier: type is of wrong type!");}
if(traindoc.size() != type.size()){
stop("SVMClassifier: traindoc.size() != type.size()!");}
int train_size = traindoc.size();
int test_size = testdoc.size();
CharacterVector all_doc(train_size + test_size);
for(int i=0; i < train_size; i++) all_doc[i] = traindoc[i];
for(int j=train_size; j < test_size + train_size; j++) all_doc[j] = testdoc[j - train_size];
List word_idf (CwordExtract(segmentCN, all_doc));
arma::sp_mat train_mat (CsenToVec(segmentCN, traindoc, word_idf));
arma::sp_mat test_mat (CsenToVec(segmentCN, testdoc, word_idf));
arma::sp_mat train_ker_mat(CkerMat(train_mat, train_mat));
arma::sp_mat test_ker_mat(CkerMat(train_mat, test_mat));
List alpha_b (CmySMO(train_ker_mat, type, c_contr, iterate, c_epsilon));
double b = alpha_b["b"] ;
NumericVector alpha = alpha_b["alpha"];
Rcout << "b is : "<< b <<std::endl;
Rcout <<"alpha size is : "<<alpha.size()<<std::endl;
arma::rowvec res (CpredictSVM(alpha_b["alpha"], alpha_b["b"], type, test_ker_mat));
return wrap(res);
}
示例4: uniques
CharacterVectorOrderer::CharacterVectorOrderer(const CharacterVector& data) :
orders(no_init(data.size()))
{
int n = data.size();
if (n == 0) return;
dplyr_hash_set<SEXP> set(n);
// 1 - gather unique SEXP pointers from data
SEXP* p_data = Rcpp::internal::r_vector_start<STRSXP>(data);
SEXP previous = *p_data++;
set.insert(previous);
for (int i = 1; i < n; i++, p_data++) {
SEXP s = *p_data;
// we've just seen this string, keep going
if (s == previous) continue;
// is this string in the set already
set.insert(s);
previous = s;
}
// retrieve unique strings from the set
int n_uniques = set.size();
LOG_VERBOSE << "Sorting " << n_uniques << " unique character elements";
CharacterVector uniques(set.begin(), set.end());
static Function sort("sort", R_BaseEnv);
CharacterVector s_uniques = Language(sort, uniques).fast_eval();
// order the uniques with a callback to R
IntegerVector o = r_match(uniques, s_uniques);
// combine uniques and o into a hash map for fast retrieval
dplyr_hash_map<SEXP, int> map(n_uniques);
for (int i = 0; i < n_uniques; i++) {
map.insert(std::make_pair(uniques[i], o[i]));
}
// grab min ranks
p_data = Rcpp::internal::r_vector_start<STRSXP>(data);
previous = *p_data++;
int o_pos;
orders[0] = o_pos = map.find(previous)->second;
for (int i = 1; i < n; ++i, ++p_data) {
SEXP s = *p_data;
if (s == previous) {
orders[i] = o_pos;
continue;
}
previous = s;
orders[i] = o_pos = map.find(s)->second;
}
}
示例5: downstreamCpp
IntegerVector downstreamCpp(IntegerMatrix AM,
CharacterVector nodeNames,
CharacterVector index){
int nN = nodeNames.size();
int nIndex = index.size();
IntegerVector whN = match(index, nodeNames);
bool miss = is_true(any(is_na(whN)));
if (miss) stop ("unmatched node provided");
IntegerVector rval(index.size(),0); // len celkovy pocet bez konkretnych vysledkov
for (int i=0; i < nIndex; i++) {
IntegerVector marked(nN);
IntegerVector distv(nN);
marked.names() = nodeNames;
distv.names() = nodeNames;
int distx = 1;
int newmk = 0;
String current = index[i];
marked[current] = 1;
bool done = false;
while(!done) {
LogicalVector selN = marked==1;
CharacterVector minds = nodeNames[selN];
//Rf_PrintValue(marked);
//Rf_PrintValue(minds);
for (int j=0; j<minds.size(); j++ ) {
// CharacterVector nodeS = minds(j);
int node = match(minds, nodeNames)(j);
IntegerVector avec = AM(node-1,_);
avec.names() = nodeNames;
LogicalVector discard = is_na(match(nodeNames,index));
avec[discard] = 0;
LogicalVector discard2 = (marked != 0) & (avec == 1);
avec[discard2] = 0;
LogicalVector avecFin = avec==1;
CharacterVector avecNames = nodeNames[avecFin];
//Rf_PrintValue(avec);
marked[avecNames] = 1;
distv[avecNames] = distx;
}
marked[minds] = 2;
distx++;
newmk = sum(marked == 1);
if (newmk == 0) done = true;
}
marked[current] = 0;
rval[i] = sum(marked==2);
}
rval.names()=index;
return(rval);
}
示例6: ctmcFit
// [[Rcpp::export]]
List ctmcFit(List data, bool byrow=true, String name="", double confidencelevel = 0.95)
{
CharacterVector stateData(as<CharacterVector>(data[0]).size());
for(int i = 0; i < as<CharacterVector>(data[0]).size(); i++)
stateData[i] = as<CharacterVector>(data[0])[i];
NumericVector transData = data[1];
CharacterVector sortedStates = unique(as<CharacterVector>(data[0])).sort();
NumericVector stateCount(sortedStates.size());
NumericVector stateSojournTime(sortedStates.size());
List dtmcData = markovchainFit(stateData, "mle", byrow, 10, 0, name, false, confidencelevel);
for(int i = 0; i < stateData.size() - 1; i++){
int idx = std::find(sortedStates.begin(), sortedStates.end(), stateData[i]) - sortedStates.begin();
stateCount[idx]++;
stateSojournTime[idx] += transData[i+1] - transData[i];
}
S4 dtmcEst = dtmcData["estimate"];
NumericMatrix gen = dtmcEst.slot("transitionMatrix");
for(int i = 0; i < gen.nrow(); i++){
for(int j = 0; j < gen.ncol(); j++){
if(stateCount[i] > 0)
gen(i, j) *= stateCount[i] / stateSojournTime[i];
}
if(stateCount[i] > 0)
gen(i, i) = - stateCount[i] / stateSojournTime[i];
else
gen(i, i) = -1;
}
double zscore = stats::qnorm_0(confidencelevel, 1.0, 0.0);
NumericVector lowerConfVecLambda(sortedStates.size()), upperConfVecLambda(sortedStates.size());
for(int i = 0; i < sortedStates.size(); i++){
if(stateCount[i] > 0){
lowerConfVecLambda(i) = std::max(0., stateCount[i] / stateSojournTime[i] * (1 - zscore / sqrt(stateCount[i])));
upperConfVecLambda(i) = std::min(1., stateCount[i] / stateSojournTime[i] * (1 + zscore / sqrt(stateCount[i])));
}
else{
lowerConfVecLambda(i) = 1;
upperConfVecLambda(i) = 1;
}
}
S4 outCtmc("ctmc");
outCtmc.slot("states") = sortedStates;
outCtmc.slot("generator") = gen;
outCtmc.slot("name") = name;
return List::create(_["estimate"] = outCtmc,
_["errors"] = List::create(_["dtmcConfidenceInterval"] = dtmcData["confidenceInterval"],
_["lambdaConfidenceInterval"] = List::create(_["lowerEndpointVector"] = lowerConfVecLambda,
_["upperEndpointVector"] = upperConfVecLambda)));
}
示例7: make_variable_column_character
CharacterVector make_variable_column_character(CharacterVector x, int nrow) {
CharacterVector output = no_init(x.size() * nrow);
int idx = 0;
for (int i = 0; i < x.size(); ++i)
for (int j = 0; j < nrow; ++j)
output[idx++] = x[i];
return output;
}
示例8: get_uniques
CharacterVector get_uniques( const CharacterVector& left, const CharacterVector& right){
int nleft = left.size(), nright = right.size() ;
int n = nleft + nright ;
CharacterVector big = no_init(n) ;
CharacterVector::iterator it = big.begin() ;
std::copy( left.begin(), left.end(), it ) ;
std::copy( right.begin(), right.end(), it + nleft ) ;
return Language( "unique", big ).fast_eval() ;
}
示例9: make_variable_column_factor
// Optimized factor routine for the case where we want to make
// a factor from a vector of names -- used for generating the
// 'variable' column in the melted data.frame
IntegerVector make_variable_column_factor(CharacterVector x, int nrow) {
IntegerVector output = no_init(x.size() * nrow);
int idx = 0;
for (int i = 0; i < x.size(); ++i)
for (int j = 0; j < nrow; ++j)
output[idx++] = i + 1;
output.attr("levels") = x;
output.attr("class") = "factor";
return output;
}
示例10: tableExpand_cpp
//[[Rcpp::export]]
std::string tableExpand_cpp(CharacterVector cells, IntegerVector colsWidth, CharacterVector justify, CharacterVector sepCols, std::string style) {
std::string res = "", word, *line;
size_t pos;
char endline = '\n';
std::vector<std::string> cellsC(cells.size()), *resSplit;
int i, j, width, maxLengthCells = 0, n = 0;
bool hasLineBreak = false;
// check for having a line break and convert to string vector for easiness
for (i = 0; i < cellsC.size(); i++){
cellsC[i] = std::string(cells[i]);
if (cellsC[i].find(endline) != std::string::npos)
hasLineBreak = true;
}
if (hasLineBreak){
if (style == "simple" || style == "rmarkdown")
stop("Pandoc does not support newlines in simple or Rmarkdown table format!");
n = cellsC.size();
resSplit = new std::vector<std::string>[n];
for (i = 0; i < n; i++){
line = &cellsC[i];
do
{
pos = line->find(endline);
if (line->substr(0, pos) != "")
resSplit[i].push_back(line->substr(0, pos));
line->erase(0, pos + 1);
}
while (pos != std::string::npos);
if (resSplit[i].size() > maxLengthCells)
maxLengthCells = resSplit[i].size();
}
for (i = 0; i < maxLengthCells; i++){
CharacterVector newCells(n);
for (j = 0; j < n; j++)
newCells[j] = resSplit[j].size() > i ? resSplit[j][i] : " ";
res += tableExpand_cpp(newCells, colsWidth, justify, sepCols, style);
if (i != (maxLengthCells - 1)) // because of collapse usage
res += '\n';
}
} else {
res = sepCols[0];
for (i = 0; i < cells.size(); i++){
width = colsWidth[i] + std::count(cells[i].begin(), cells[i].end(), '\\');
std::string fres = format_cpp(as<std::string>(cells[i]), as<std::string>(justify[i]), width);
res += fres;
if (i != (cells.length() - 1)) // because of collapse usage
res += sepCols[1];
}
res += sepCols[2];
}
return res;
}
示例11: IBDMatrix
// Internal Function
//
// Binary Matrix of IBD
//
// Creates a binary matrix of IBD (1) and non-IBD (0) with each row representing a single SNP
// and each column representing a unique pair. The number of rows is equal to the total
// number of SNPs and the number of columns is equal to the number of pairs.
//
// @param chromosomes A character vector containing the corresponding chromosome for each SNP
// @param positions_bp A numeric vector containing the corresponding bp position for each SNP
// @param number_pairs Numeric. The total number of pairs analysed
// @param ibd_pairs_colnumbers A numeric vector corresponding to column numbers in the output matrix
// where each unique number refers to a unique pair with IBD inferred
// @param ibd_chromosomes A character vector containing the chromosome for each detected IBD segment
// @param ibd_start_bp A numeric vector containing the base-pair position for the start of each detected IBD segment
// @param ibd_stop_bp A numeric vector containing the base-pair position for the end of each detected IBD segment
// [[Rcpp::export]]
IntegerMatrix IBDMatrix(CharacterVector chromosomes, NumericVector positions_bp, int number_pairs, IntegerVector ibd_pairs_colnumbers,
CharacterVector ibd_chromosomes, NumericVector ibd_start_bp, NumericVector ibd_stop_bp) {
IntegerMatrix ibd_m(chromosomes.size(), number_pairs);
for(int i=0; i<ibd_chromosomes.size(); i++){
for(int j=0; j<chromosomes.size(); j++){
if(chromosomes[j] == ibd_chromosomes[i] && positions_bp[j] >= ibd_start_bp[i] && positions_bp[j] <= ibd_stop_bp[i]){
ibd_m(j,ibd_pairs_colnumbers[i]-1) = 1;
}
}
}
return ibd_m;
}
示例12: ex10
// [[Rcpp::export]]
SEXP ex10(DataFrame input){
CharacterVector names = input.names();
for(int i = 0; i<names.size(); i++){
cout << "names[" << i << "]=" << names[i] << "\n";
}
return(names);
}
示例13: 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 ;
}
示例14: ch5ChunkSel
// Function to return a selected part of a data frame as a list
List ch5ChunkSel(string chunkName, CharacterVector selCols, string filePath)
{
// Open the file in Read/Write Mode, H5F_ACC_RDONLY
H5File *file = new H5File(filePath, H5F_ACC_RDONLY);
// Opening the data set
DataSet dataset = file->openDataSet((H5std_string)chunkName);
// Opening the data space
DataSpace dataspace = dataset.getSpace();
// Get the number of dimensions
int ndim = dataspace.getSimpleExtentNdims();
// Create a dimension object to be filled with the dimensions of the data set
hsize_t dims[ndim];
// Fill the dimension of the dataset
dataspace.getSimpleExtentDims(dims, NULL);
// Create the return data
// Filling the matrix with data form the dataspace
SEXP data;
// Allocating a matrix of the right size and dimension
data = PROTECT(Rf_allocMatrix(REALSXP, dims[0], dims[1]));
// Filling the matrix with data form the dataspace
dataset.read(REAL(data), PredType::NATIVE_DOUBLE, dataspace);
UNPROTECT(1);
// converting the R object to a numeric matrix
NumericMatrix M = as<NumericMatrix>(data);
CharacterVector colNames = ch5ReadCharVector("ColumnNames", filePath);
CharacterVector colClasses = ch5ReadCharVector("ColumnClasses", filePath);
// Create the output
List DF;
string colName;
string colClass;
NumericVector vec;
CharacterVector levels;
int n = selCols.size();
IntegerVector sel(n);
int selN;
// First we need to find which of the columns has been selected
sel = match(selCols, colNames);
for(int i = 0; i < n; i++)
{
colName = selCols[i];
selN = sel[i] - 1;
colClass = colClasses[selN];
if(colClass != "factor")
{
DF[colName] = M(_, selN);
}else{
vec = M(_, selN);
levels = (CharacterVector)ch5ReadFactor(colName, filePath);
DF[colName] = cCreateFactor(vec, levels);
}
}
dataset.close();
file->close();
return DF;
}
示例15: count_clusters
void count_clusters(std::map<std::string, int> &counts,
CharacterVector these_clusters,
CharacterVector cluster_names) {
int n = these_clusters.size();
for(int i=0; i < n; i++) {
counts[as<std::string>(these_clusters[i])]++;
}
//print_cluster_counts(counts);
// add clusters with no counts
CharacterVector::iterator it;
for(it = cluster_names.begin(); it != cluster_names.end(); ++it) {
// if this name not in map, make an entry and set it to zero
if(counts.find(as<std::string>(*it)) == counts.end()) {
counts[as<std::string>(*it)] = 0;
}
}
//print_cluster_counts(counts);
return;
}