<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Nathan VanHoudnos &#187; rstats</title>
	<atom:link href="http://www.stat.cmu.edu/~nmv/tag/rstats/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.stat.cmu.edu/~nmv</link>
	<description>Statistics and Public Policy Ph.D. Student</description>
	<lastBuildDate>Thu, 07 Feb 2013 20:43:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
		<item>
		<title>R is not C</title>
		<link>http://www.stat.cmu.edu/~nmv/2011/12/07/r-is-not-c/</link>
		<comments>http://www.stat.cmu.edu/~nmv/2011/12/07/r-is-not-c/#comments</comments>
		<pubDate>Wed, 07 Dec 2011 14:14:52 +0000</pubDate>
		<dc:creator>nmv</dc:creator>
				<category><![CDATA[rstats]]></category>

		<guid isPermaLink="false">http://nathanvan.wordpress.com/?p=215</guid>
		<description><![CDATA[I keep trying to write R code like it was C code. It is a habit I&#8217;m trying to break myself of. For example, the other day I need to construct a model matrix of 1&#8242;s and 0&#8242;s in the &#8230; <a href="http://www.stat.cmu.edu/~nmv/2011/12/07/r-is-not-c/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I keep trying to write R code like it was C code. It is a habit I&#8217;m trying to break myself of. </p>
<p>For example, the other day I need to construct a model matrix of 1&#8242;s and 0&#8242;s in the standard, counting in binary, pattern. My solution was:<br />
<code>
<pre>
n &lt;- 8
powers &lt;- 2^(0:(n-1))
NN &lt;- (max(powers)*2)
designMatrix &lt;- matrix( NA, nrow=NN, ncol=n)
for( ii in 0:(NN-1) ) {
     leftOver &lt;- ii
     for ( jj in 1:n ) {
          largest &lt;- rev(powers)[jj]
          if ( leftOver != 0 &amp;&amp; largest &lt;= leftOver ) {
               designMatrix[ii+1,jj] &lt;- 1	
               leftOver &lt;- leftOver - largest
          } else {
               designMatrix[ii+1,jj] &lt;- 0
          }
     }	
} 
print(designMatrix)
</pre>
<p></code></p>
<p>The code works, but it is a low-level re-implementation of something that already exists in base R. R is not C, because base R has pieces that implement statistical ideas for you. Consider:<br />
<code>
<pre>
expand.grid                package:base                R Documentation

Create a Data Frame from All Combinations of Factors

Description:

     Create a data frame from all combinations of the supplied vectors
     or factors.  See the description of the return value for precise
     details of the way this is done.
</pre>
<p></code></p>
<p>So then instead of writing (and debugging!) a function to make a binary model matrix, I could have simply used a one-liner:<br />
<code>
<pre>
# Note that c(0,1) is encased in list() so that
# rep(..., n) will repeat the object c(0,1) n 
# times instead of its default behavior of 
# concatenating the c(0,1) objects. 
designMatrix_R &lt;- as.matrix( expand.grid( rep( list(c(0,1) ), n) ) )
</pre>
<p></code></p>
<p>I like it. It is both shorter and easier to debug. Now I just need to figure out how to <em>find</em> these base R functions before I throw up my hands and re-implement them in C. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.stat.cmu.edu/~nmv/2011/12/07/r-is-not-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>