Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Having written tens of thousands of lines of code in both languages, R is just a lot more idiosyncratic and less consistent than Python. It's really nice for doing quick plots and investigation on a small to medium size dataset, mainly because its built-in dataframe object is nicer than the numpy/scipy equivalent, but you'll find all kinds of weirdness as you do things more complicated than one liners. My advice is to use <a href="http://rpy.sourceforge.net/rpy2.html">rpy2</a> (which unfortunately has a much worse UI than its predecessor, rpy) and just do as little as possible in R with the rest in Python.</p> <p>For example, consider the following matrix code:</p> <pre><code>&gt; u = matrix(1:9,nrow=3,ncol=3) &gt; v = u[,1:2] &gt; v[1,1] [2] 1 &gt; w = u[,1] &gt; w[1,1] Error in w[1, 1] : incorrect number of dimensions </code></pre> <p>How did that fail? The reason is that if you select a submatrix from a matrix which has only one column along any given axis, R "helpfully" drops that column and changes the type of the variable. So w is a vector of integers rather than a matrix: </p> <pre><code>&gt; class(v) [1] "matrix" &gt; class(u) [1] "matrix" &gt; class(w) [1] "integer" </code></pre> <p>To avoid this, you need to actually pass an obscure keyword parameter:</p> <pre><code>&gt; w2 = u[,1,drop=FALSE] &gt; w2[1,1] [3] 1 &gt; class(w2) [1] "matrix" </code></pre> <p>There's a lot of nooks and crannies like that. Your best friend at the beginning will be introspection and online help tools like <code>str</code>,<code>class</code>,<code>example</code>, and of course <code>help</code>. Also, make sure to look at the example code on the <a href="http://addictedtor.free.fr/graphiques/thumbs.php">R Graph Gallery</a> and in Ripley's <a href="http://www.stats.ox.ac.uk/pub/MASS4/">Modern Applied Statistics with S-Plus</a> book. </p> <hr> <p><strong>EDIT</strong>: Here's another great example with factors. </p> <pre><code>&gt; xx = factor(c(3,2,3,4)) &gt; xx [1] 3 2 3 4 Levels: 2 3 4 &gt; yy = as.numeric(xx) &gt; yy [1] 2 1 2 3 </code></pre> <p>Holy cow! Converting something from a factor back to a numeric didn't actually do the conversion you thought it would. Instead it's doing it on the internal enumerated type of the factor. This is a source of hard-to-find bugs for people who aren't aware of this, because it's still returning integers and will in fact actually work <em>some</em> of the time (when the input is already numerically ordered). </p> <p>This is what you actually need to do</p> <pre><code>&gt; as.numeric(levels(xx))[xx] [1] 3 2 3 4 </code></pre> <p>Yeah, sure, that fact is on the <code>factor</code> help page, but you only land up there when you've lost a few hours to this bug. This is another example of how R does not do what you intend. Be very, very careful with anything involving type conversions or accessing elements of arrays and lists.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload