Skip to content
Prev 663 / 10988 Next

[Rcpp-devel] Rcpp::DataFrame and factors

Hi,

The new Rcpp::DataFrame follows R's tradition and automatically converts 
character vectors into factors:

 > fun <- cppfunction( signature(), '
+         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(Named("a")=v, Named("b")=s);
+ ' )
 > fun()
   a b
1 1 a
2 2 b
3 3 c
 > fun()[["b"]]
[1] a b c
Levels: a b c


Is this good or bad ? I think it is bad as we don't have a way to turn 
this off... we can set the options( "stringsAsFactors" ) to FALSE

Should we allow something like this:

DataFrame::create(
	_["x"] = v ,
	_["y"] = s,
	_["stringsAsFactors"] = false )

or maybe have a variable like this to pilot the option:

static bool DataFrame::stringsAsFactors = false ;

Romain