title: R Error: replacement diagonal has wrong length
body: <p>I am trying to debug this line of R code, which was written in and works with R version 2.3.x and called in using rpy but now moving to 2.15.3, and rpy2, it seems to be broken? My matrix is quite large, it is a length 409x409., and the diagonals are filled with floating integers (I think?).  I am looking at the raw matrix file that is pulled in and the diags are filled with 99999.00.  The rest of the cells are in the format of ##.##</p>
  
  <p>The offending line of code in question:</p>
  
  <pre><code>diag(mymatrix) &lt;- apply(mymatrix, 1, function(x) min(x[x&gt;0], na.rm=T)) * 0.73
  </code></pre>
  
  <p>returns the following error:</p>
  
  <blockquote>
    <p>Traceback (most recent call last):
      File "", line 1, in 
      File "C:\Python27\lib\site-packages\rpy2\robjects\functions.py", line 166, in <strong>call</strong>
        return super(SignatureTranslatedFunction, self).<strong>call</strong>(*args, **kwargs)
      File "C:\Python27\lib\site-packages\rpy2\robjects\functions.py", line 99, in <strong>call</strong>
        res = super(Function, self).<strong>call</strong>(*new_args, **new_kwargs)
    RRuntimeError: Error in <code>diag&lt;-</code>(<code>*tmp*</code>, value = c(72999.27, 1.39576, 2.79616323829879,  : 
      replacement diagonal has wrong length</p>
  </blockquote>
  
  <p>I looked at <a href="https://svn.r-project.org/R/trunk/src/library/base/R/diag.R" rel="nofollow">the SVN.R website</a> to see the inner workings of the <code>diag</code>function, to see where the error is coming from:</p>
  
  <pre><code>if (len.v != 1L &amp;&amp; len.v != len.i)
  stop("replacement diagonal has wrong length")
  </code></pre>
  
  <p>I think that tells me my answer right there, my replacement value is not the proper length, right?  So how do I go about getting my replacement diagonal to the proper length for this to "stick"? </p>
  
  <p><strong>EDIT --Added context for troubleshooting purposes--</strong></p>
  
  <p>This matrix is initially generated as an 409x409 array (using numpy) through a Python script and assigned using <code>ro.r.assign</code>.  Then, in my R script, <code>mymatrix</code> was originally (in the R version 2.3.1 form) called in using:</p>
  
  <p><code>mymatrix &lt;- do.call("rbind", mymatrix)</code></p>
  
  <p>But now in 2.15.3 and Rpy2, I got an error using this do.call on the array.  It would give me the following error:</p>
  
  <blockquote>
    <p>File "", line 1, in 
      File "C:\Python27\lib\site-packages\rpy2\robjects\functions.py", line 166, in <strong>call</strong>
        return super(SignatureTranslatedFunction, self).<strong>call</strong>(*args, **kwargs)
      File "C:\Python27\lib\site-packages\rpy2\robjects\functions.py", line 99, in <strong>call</strong>
        res = super(Function, self).<strong>call</strong>(*new_args, **new_kwargs)
    RRuntimeError: Error in do.call("rbind", mymatrix) : second argument must be a list</p>
  </blockquote>
  
  <p>So my "fix" for this was to simply use <code>as.list</code> to convert it to a list as written below:</p>
  
  <p><code>mymatrix &lt;- do.call("rbind", as.list(mymatrix))</code> </p>
  
  <p>But now thinking about this further, it occurs to me that maybe this is the problem.  Do I need to convert this back again for the diagonal to work correctly, and if so, how?</p>
  
  <p>Thanks!</p>