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

Romain Francois romain at r-enthusiasts.com
Wed Jun 26 11:44:23 CEST 2013


Thanks for this. We'll have a look.

Romain

Le 25/06/13 22:06, Kevin Ushey a écrit :
> 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


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30

R Graph Gallery: http://gallery.r-enthusiasts.com

blog:            http://blog.r-enthusiasts.com
|- http://bit.ly/13SrjxO : highlight 0.4.2
`- http://bit.ly/10X94UM : Mobile version of the graph gallery



More information about the Rcpp-devel mailing list