#include <Rcpp.h> // [[Rcpp::export]] Rcpp::NumericMatrix myBinner(Rcpp::NumericVector myVect, float bin_width = 0.2){ int i = 0; int j = 0; int nbins = 1/bin_width; Rcpp::NumericMatrix breaks( nbins, 4 ); int multiplier = 1000; Rcpp::IntegerMatrix intBreaks( nbins, 4 ); Rcpp::StringVector colnames(4); colnames(0) = "START"; colnames(1) = "MID"; colnames(2) = "END"; colnames(3) = "COUNT"; Rcpp::colnames(breaks) = colnames; Rcpp::colnames(intBreaks) = colnames; // Rcpp::IntegerVector counts(nbins); breaks(0,0) = 0; breaks(0,1) = bin_width/2; breaks(0,2) = bin_width; intBreaks(0,0) = 0; intBreaks(0,1) = (bin_width/2) * multiplier; intBreaks(0,2) = bin_width * multiplier; for(i=1; i<breaks.nrow(); i++){ breaks(i,0) = breaks(i-1,0) + bin_width; breaks(i,1) = breaks(i-1,1) + bin_width; breaks(i,2) = breaks(i-1,2) + bin_width; intBreaks(i,0) = intBreaks(i-1,0) + bin_width * multiplier; intBreaks(i,1) = intBreaks(i-1,1) + bin_width * multiplier; intBreaks(i,2) = intBreaks(i-1,2) + bin_width * multiplier; } Rcpp::Rcout << "\nBinning!\n\n"; for(i=0; i<myVect.size(); i++){ int intQuery = myVect(i) * multiplier; j = 0; if( intQuery >= intBreaks(j,0) & intQuery <= intBreaks(j,2) ){ // if( myVect(i) >= breaks(j,0) & myVect(i) <= breaks(j,2) ){ Rcpp::Rcout << "Binned: " << myVect(i) << " is >= " << breaks(j,0) << " & <= " << breaks(j,2) << "\n"; breaks(j,3) = breaks(j,3) + 1; } for(j=1; j<breaks.nrow(); j++){ if( intQuery > intBreaks(j,0) & intQuery <= intBreaks(j,2) ){ // if( myVect(i) >= breaks(j,0) & myVect(i) <= breaks(j,2) ){ Rcpp::Rcout << "Binned: " << myVect(i) << " is > " << breaks(j,0) << " & <= " << breaks(j,2) << "\n"; breaks(j,3) = breaks(j,3) + 1; } } } return(breaks); }
set.seed(9) #x <- runif(n=4) # binWidth <- 0.02 #binWidth <- 0.2 x <- c(0, 0.2, 0.4, 0.8, 1.0) counts <- myBinner(x, binWidth) myHist <- hist(x, plot = FALSE, breaks = seq(0,1,by=binWidth)) # cbind(counts, myHist$counts)
#include <Rcpp.h> // [[Rcpp::export]] void myFunction( double bin_width = 0.2 ){ int nbins = 1/bin_width; int i = 0; Rcpp::NumericVector query(1); // query(0) = 0.1; // query(0) = 0.2; query(0) = 0.4; Rcpp::NumericVector breaks( nbins + 1 ); breaks(0) = 0; for(i=1; i<breaks.size(); i++){ breaks(i) = breaks(i-1) + bin_width; } Rcpp::Rcout << "Testing: \n"; for(i=0; i < breaks.size() - 1; i++){ if( query(0) > breaks(i) & query(0) <= breaks(i+1) ){ Rcpp::Rcout << "Binned: " << query(0) << " is > " << breaks(i) << " and <= " << breaks(i+1) << "\n"; } } }
myFunction( bin_width = 0.2) myFunction( bin_width = 0.1) myFunction( bin_width = 0.05) myFunction( bin_width = 0.02) myFunction( bin_width = 0.01)
#include <Rcpp.h> // [[Rcpp::export]] void myIntFunction( double bin_width = 0.2 ){ int nbins = 1/bin_width; int i = 0; int multiplier = 1000; Rcpp::NumericVector query(1); // query(0) = 0.1; // query(0) = 0.2; query(0) = 0.4; // Initialize breaks. Rcpp::NumericVector breaks( nbins + 1 ); Rcpp::IntegerVector intBreaks( nbins + 1 ); breaks(0) = 0; intBreaks(0) = 0; for(i=1; i<breaks.size(); i++){ breaks(i) = breaks(i-1) + bin_width; intBreaks(i) = intBreaks(i-1) + bin_width * multiplier; } Rcpp::Rcout << "Testing: \n"; for(i=0; i < breaks.size() - 1; i++){ // Rcpp::as< int >(StringV(i)); int intQuery = query(0) * multiplier; // intQuery = intQuery * multiplier; if( intQuery > intBreaks(i) & intQuery <= intBreaks(i+1) ){ // if( query(0) > breaks(i) & query(0) <= breaks(i+1) ){ Rcpp::Rcout << "Binned: " << query(0) << " is > " << breaks(i) << " and <= " << breaks(i+1) << "\n"; } } }
myIntFunction( bin_width = 0.2) myIntFunction( bin_width = 0.1) myIntFunction( bin_width = 0.05) myIntFunction( bin_width = 0.02) myIntFunction( bin_width = 0.01)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.