<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style>
<!--
@font-face
{font-family:"Cambria Math"}
@font-face
{font-family:Calibri}
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri","sans-serif"}
a:link, span.MsoHyperlink
{color:blue;
text-decoration:underline}
a:visited, span.MsoHyperlinkFollowed
{color:purple;
text-decoration:underline}
span.EmailStyle17
{font-family:"Calibri","sans-serif";
color:windowtext}
.MsoChpDefault
{font-family:"Calibri","sans-serif"}
@page WordSection1
{margin:1.0in 1.0in 1.0in 1.0in}
div.WordSection1
{}
-->
</style>
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal">Hi,</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">I have a need to loop through all the entries of a DataFrame by row, then column. I know two different ways:</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"> // Case A: When df.length() is unknown at coding time:</p>
<p class="MsoNormal"> int n = df.nrows();</p>
<p class="MsoNormal"> int m = df.length();</p>
<p class="MsoNormal"> for(int i=1; i<n; i++) {</p>
<p class="MsoNormal"> for(int j=0; j<m; j++) {</p>
<p class="MsoNormal"> NumericVector v = df[j];</p>
<p class="MsoNormal"> // ... do stuff with v[i] ...</p>
<p class="MsoNormal"> }</p>
<p class="MsoNormal"> }</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal"> // Case B: If I know the number of columns while writing the C code:</p>
<p class="MsoNormal"> int n = df.nrows();</p>
<p class="MsoNormal"> NumericVector xs = df[0];</p>
<p class="MsoNormal"> NumericVector ys = df[1];</p>
<p class="MsoNormal"> for(int i=1; i<n; i++) {</p>
<p class="MsoNormal"> // ... do stuff with xs[i] and ys[i] ...</p>
<p class="MsoNormal"> }</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">The second way is less flexible, but it’s also quite a bit faster in practice – I presume this means the "NumericVector ..." expressions are doing a non-trivial amount of work (perhaps even copying the whole vector?).</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">Is there a way to have my cake & eat it? Can I efficiently (O[1]) index into a DataFrame by numeric row index and numeric column index?</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">I’m also curious why it’s a syntax error in Case A to just write `df[j][i]` or even `((NumericVector) df[j])[i]` – clearly there’s magic behind the "NumericVector" call that I don’t understand.</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">Thanks.</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">--</p>
<p class="MsoNormal">Ken Williams, Senior Research Scientist</p>
<p class="MsoNormal"><b><i><span style="color:#365F91">Wind</span></i></b><b><i><span style="color:#76923C">Logics</span></i></b><b><i></i></b></p>
<p class="MsoNormal">http://windlogics.com</p>
<p class="MsoNormal"> </p>
</div>
<br>
<hr>
<font face="Verdana" color="Blue" size="2"><br>
CONFIDENTIALITY NOTICE: This e-mail message is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized review, use, disclosure or distribution of any kind is strictly prohibited. If you are not
the intended recipient, please contact the sender via reply e-mail and destroy all copies of the original message. Thank you.<br>
</font>
</body>
</html>