Skip to content

[Rcpp-devel] How to set stringsAsFactors=FALSE in Rcpp::DataFrame?

3 messages · Dirk Eddelbuettel, Asis Hallab

#
Dear Rcpp-Experts,

how can I get the equivalent of the R expression

my.df <- data.frame( ?, stringsAsFactors=FALSE )

in Rcpp?

I have this code in Rcpp:
   ?
   return( DataFrame::create(
      Named( "acc" )       = accsCol,
      Named( "ec" )        = ecCol,
      Named( "prot.acc" )  = protCol,
      Named( "term_type" ) = termTypeCol
    ) );

Calling the above from R, I realize, that the returned DataFrame's
columns are factors and not character-vectors, as I need them to be.

How to achieve this?
Should I just convert them in R itself, like in the following example?

for ( i in 1:ncol(my.df) ) {
  my.df[,i] <- as.character( my.df[,i] )
}

Or is there a better - and possibly more efficient - way to achieve this?

As always your help will be much appreciated.

Have a pleasant day and
Cheers!
#
On 22 July 2014 at 12:59, Asis Hallab wrote:
| Dear Rcpp-Experts,
| 
| how can I get the equivalent of the R expression
| 
| my.df <- data.frame( ?, stringsAsFactors=FALSE )
| 
| in Rcpp?

The same way, but setting a variable name stringsAsFactors to FALSE, or
rather its lowercase C++ equivalent.

In cases like this it is a good idea to peruse the unit tests where you would
have found

// [[Rcpp::export]]
DataFrame createTwoStringsAsFactors(){
    IntegerVector v = IntegerVector::create(1,2,3);
    std::vector<std::string> s(3);
    s[0] = "a";
    s[1] = "b";
    s[2] = "c";
    return DataFrame::create(_["a"] = v,
                             _["b"] = s,
                             _["stringsAsFactors"] = false );
}

I prefer Named("....") over _["..."] but it amounts to the same.

Dirk


| 
| I have this code in Rcpp:
|    ?
|    return( DataFrame::create(
|       Named( "acc" )       = accsCol,
|       Named( "ec" )        = ecCol,
|       Named( "prot.acc" )  = protCol,
|       Named( "term_type" ) = termTypeCol
|     ) );
| 
| Calling the above from R, I realize, that the returned DataFrame's
| columns are factors and not character-vectors, as I need them to be.
| 
| How to achieve this?
| Should I just convert them in R itself, like in the following example?
| 
| for ( i in 1:ncol(my.df) ) {
|   my.df[,i] <- as.character( my.df[,i] )
| }
| 
| Or is there a better - and possibly more efficient - way to achieve this?
| 
| As always your help will be much appreciated.
| 
| Have a pleasant day and
| Cheers!
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
#
Dear Dirk,

many thanks for the provided solution.

Cheers!

2014-07-22 13:35 GMT+02:00 Dirk Eddelbuettel <edd at debian.org>: