[Rcpp-devel] data.frame from R to C++?

Dirk Eddelbuettel edd at debian.org
Wed Apr 13 04:15:27 CEST 2011


On 12 April 2011 at 15:04, deqiang sun wrote:
| Thanks, the stringsAsFactors=FALSE option works.

And Rcpp 0.9.4 is now on CRAN which contains a fix Romain but that no longer
requires it.

Dirk

 
| Dsun
| On Apr 12, 2011, at 6:36 AM, Dirk Eddelbuettel wrote:
| 
| > 
| > On 12 April 2011 at 05:38, deqiang sun wrote:
| > | How should I read the txt in the following context?
| > | c3	3a
| > | c1	1b
| > | c2	2c
| > | 
| > | The code I have is 
| > | 		RInside R(argc, argv);
| > | 		SEXP ans;
| > | 
| > | 		std::string txt = "a=read.csv('xxx.xls',sep='\t',head=F)";
| > | 		ans = R.parseEval(txt);
| > | 		Rcpp::DataFrame DF(ans);
| > | 
| > | 		Rcpp::StringVector x1 = DF["V1"];
| > | 		Rcpp::StringVector x2 = DF["V2"];
| > | 
| > | But the print of x1 and x2 is 
| > | 3	3	
| > | 1	1	
| > | 2	2	
| > | 
| > | What's wrong with my code?
| > 
| > The read.csv() function and its cousins default to stringsAsFactors=TRUE. 
| > 
| > So the '3 1 2' are the factor levels. You want either 
| > 
| >    std::string txt = "a=read.csv('xxx.xls',sep='\t',"
| >                      "head=FALSE,stringsAsFactors=FALSE)";
| > 
| > or set it as a global option.  Google for stringsAsFactors, this is
| > documented and are ways around it.  
| > 
| > Dirk
| > 
| > PS  I personally would also set column names in the data file or R code.
| > 
| > 
| > | Thanks,
| > | 
| > | Dsun
| > | On Apr 12, 2011, at 4:14 AM, Romain Francois wrote:
| > | 
| > | > Le 12/04/11 10:57, deqiang sun a écrit :
| > | >> Hi Dirk,
| > | >> 
| > | >> Thanks very much for your example. From this example I learned how to passing data back and forth between R and C++.
| > | >> The example is R code and uses piece of C++ code in side R program.
| > | >> Well, it's better if you put this example(by only making slight changes to make R embeded in C++) inside package RInside.
| > | >> 
| > | >> Accessing data by column name is the feature I ( and other people) usually want.
| > | >> However, is there a way to access the data Frame by row number?
| > | > 
| > | > No. You access the column, and then do whatever with each element, as in 
| > | > Dirk's example.
| > | > 
| > | >> Regards.
| > | >> 
| > | >> Dsun
| > | >> On Apr 8, 2011, at 9:56 AM, Dirk Eddelbuettel wrote:
| > | >> 
| > | >>> 
| > | >>> Ok, I now committed a slightly nice example to RcppExamples, a package I
| > | >>> should revamp to contain more examples.  In there, we now receive a
| > | >>> data.frame, operate on each column, create a new data and return old and new.
| > | >>> 
| > | >>> The core of the code (inside the try/catch protection) is
| > | >>> 
| > | >>>      // construct the data.frame object
| > | >>>      Rcpp::DataFrame DF = Rcpp::DataFrame(Dsexp);
| > | >>> 
| > | >>>      // and access each column by name
| > | >>>      Rcpp::IntegerVector a = DF["a"];
| > | >>>      Rcpp::CharacterVector b = DF["b"];
| > | >>>      Rcpp::DateVector c = DF["c"];
| > | >>> 
| > | >>>      // do something
| > | >>>      a[2] = 42;
| > | >>>      b[1] = "foo";
| > | >>>      c[0] = c[0] + 7;                      // move up a week
| > | >>> 
| > | >>>      // create a new data frame
| > | >>>      Rcpp::DataFrame NDF =
| > | >>> 	  Rcpp::DataFrame::create(Rcpp::Named("a")=a,
| > | >>> 				  Rcpp::Named("b")=b,
| > | >>> 				  Rcpp::Named("c")=c);
| > | >>> 
| > | >>>      // and return old and new in list
| > | >>>      return(Rcpp::List::create(Rcpp::Named("origDataFrame")=DF,
| > | >>> 				Rcpp::Named("newDataFrame")=NDF));
| > | >>> 
| > | >>> 
| > | >>> Dirk
| > | >>> --
| > | >>> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
| > | > 
| > | > -- 
| > | > Romain Francois
| > | > Professional R Enthusiast
| > | > +33(0) 6 28 91 30 30
| > | > http://romainfrancois.blog.free.fr
| > | > http://romain-francois.com
| > | > |- http://bit.ly/fhqbRC : Rcpp workshop in Chicago on April 28th
| > | > |- http://bit.ly/dFyZGB : Hydraulique au Montpellier Comedie Club
| > | > `- http://bit.ly/eVXit9 : Eponyme : 40 minutes stand up
| > | > 
| > | > 
| > | 
| > 
| > -- 
| > Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
| 

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list