[Rcpp-devel] two minor bugs with Rcpp's sugar table

Kevin Ushey kevinushey at gmail.com
Tue Jun 25 22:06:43 CEST 2013


Hi guys,

Sorry to be bringing this up so close after the CRAN release, but I just
stumbled upon them today.

-----

1) The table function occasionally produces names as '-0', rather than '0',
for numeric vectors (haven't tested in other situations yet):

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
IntegerVector counts(NumericVector x) {
  return table(x);
}

/*** R
set.seed(123)
x <- round( rnorm(100), 0 )
counts(x) ## names: -2, -1, -0, 1, 2

y <- round( rnorm(1000), 0 )
counts(y) ## names: -3, -2, -1, 0, 1, 2, 3, as expected
*/

-----

2) The table function doesn't return the correct names for factors:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
IntegerVector counts(IntegerVector x) {
  return table(x);
}

/*** R
set.seed(123)
x <- factor( round( rnorm(100), 0 ) )
counts(x) ## names: 1, 2, 3, 4, 5; should be -2, -1, 0, 1, 2
*/

-----

1) is really strange and I'm not sure why it's occurring; 2) is due to the
internal codes of a factor being integers, and those being passed along to
the names. This could be easily fixed by checking if x is a factor and
setting the names attribute to be the levels of the input object in that
case.

> sessionInfo()
R version 3.0.0 (2013-04-03)
Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:
[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] Rcpp_0.10.4 tools_3.0.0

As always, thanks for all the work on Rcpp. The reason why I'm so invested
in the table sugar function is because it's up to 100x faster than the base
R table function, and hence is great for 1D summaries of large vectors.

-Kevin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130625/786894f3/attachment.html>


More information about the Rcpp-devel mailing list