<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif";
        mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri","sans-serif";
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;
        font-family:"Calibri","sans-serif";
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:70.85pt 70.85pt 70.85pt 70.85pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-AU" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal">By my understanding, Rcpp is better suited to working with data-frames as columns rather than working with data-frames as a set of rows. However, occasionally it may be useful to work with the set of rows. How have others considered this
 use case? <o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">[As a motivating example based on simulations in C++, we want to pass data-frames from R for use as look-up tables in C++ (cf. passing transformed data back to R). The STL container std::map is well suited to this task, particularly as
 it provides ordered keys.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">In the following code, we define a template class Table1D which reads in a data-frame and defines the map key with the first column and the map value with the second column. The only sophistication here is (i) using std::greater as a comparison
 function and (ii) using Rcpp traits. Note that calling the function from R is only shown for demonstration.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">require(inline)<o:p></o:p></p>
<p class="MsoNormal">lookup <- rcpp(signature(df="data.frame",x="numeric"),<o:p></o:p></p>
<p class="MsoNormal">            body="<o:p></o:p></p>
<p class="MsoNormal">  Table1D<double,double> table = Table1D<double,double>(df);<o:p></o:p></p>
<p class="MsoNormal">  return wrap(table(as<double>(x)));<o:p></o:p></p>
<p class="MsoNormal">",<o:p></o:p></p>
<p class="MsoNormal">            includes="<o:p></o:p></p>
<p class="MsoNormal">#include <map><o:p></o:p></p>
<p class="MsoNormal">#include <functional><o:p></o:p></p>
<p class="MsoNormal">template <class Index, class Outcome><o:p></o:p></p>
<p class="MsoNormal">class Table1D {<o:p></o:p></p>
<p class="MsoNormal">public:<o:p></o:p></p>
<p class="MsoNormal">  std::map<Index,Outcome,std::greater<Index> > data;<o:p></o:p></p>
<p class="MsoNormal">  Table1D(DataFrame df, int iIndex = 0, int iOutcome = 1) { <o:p>
</o:p></p>
<p class="MsoNormal">    Vector<Rcpp::traits::r_sexptype_traits<Index>::rtype> df0 = df[iIndex];<o:p></o:p></p>
<p class="MsoNormal">    Vector<Rcpp::traits::r_sexptype_traits<Outcome>::rtype> df1 = df[iOutcome];<o:p></o:p></p>
<p class="MsoNormal">    for (size_t i=0; i<df0.size(); i++) {<o:p></o:p></p>
<p class="MsoNormal">      data[df0[i]] = df1[i];<o:p></o:p></p>
<p class="MsoNormal">    }<o:p></o:p></p>
<p class="MsoNormal">  }<o:p></o:p></p>
<p class="MsoNormal">  virtual Outcome lookup(Index index) {<o:p></o:p></p>
<p class="MsoNormal">    return data.lower_bound(index)->second;<o:p></o:p></p>
<p class="MsoNormal">  }<o:p></o:p></p>
<p class="MsoNormal">  virtual Outcome operator()(Index index) {<o:p></o:p></p>
<p class="MsoNormal">    return lookup(index);<o:p></o:p></p>
<p class="MsoNormal">  }<o:p></o:p></p>
<p class="MsoNormal">};<o:p></o:p></p>
<p class="MsoNormal">")<o:p></o:p></p>
<p class="MsoNormal">lookup(data.frame(as.numeric(1:1000000),10.0*as.numeric(1:1000000)), 12345.5)<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">See also <a href="https://github.com/mclements/microsimulation/blob/master/src/rcpp_table.h">
https://github.com/mclements/microsimulation/blob/master/src/rcpp_table.h</a> for an extension to higher dimensions.]<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Sincerely, Mark.<o:p></o:p></p>
</div>
</body>
</html>