[R] Infelicity in print output with matrix indexing of `[.data.frame`

David Winsemius dwinsemius at comcast.net
Sun Dec 18 19:00:45 CET 2016


> On Dec 17, 2016, at 3:15 PM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:
> 
> No, cannot agree. The result of using an n by 2 matrix to index into a rectangular object is a vector. A vector can only have one storage mode for all elements. Some type coercion is necessary to accommodate this.

I have no argument with the premise that an atomic vector must be of a single mode.  But the exact same values were established with a numeric vector into those positions indexed by the 2-column matrix. Why does extraction need to coerce the entire dataframe to matrix when none of the extracted values are character? I suppose my request is that the very simple line in `[.data.frame`


    if (is.matrix(i)) 
            return(as.matrix(x)[i])

If it were replaced by code that would only extract from the values needed and then use a shifted version of the selection matrix, you could get values that were not coerced by being innocent bystanders of a dataframe colum that was not relevant.

as.matrix( x[ min( i[ , 1]):max( i[ , 1]), min( i[ ,2 ]):max(i[ , 2]) ])[
                   cbind( i[,1]-min( i[ , 1]) +1, i[,2]- min( i[ ,2 ]) +1) ]

-- 
David.

> -- 
> Sent from my phone. Please excuse my brevity.
> 
> On December 17, 2016 2:58:24 PM PST, David Winsemius <dwinsemius at comcast.net> wrote:
>> 
>>> On Dec 17, 2016, at 11:34 AM, Bert Gunter <bgunter.4567 at gmail.com>
>> wrote:
>>> 
>>> It's documented, David:
>>> 
>>> From ?"[.data.frame"
>>> 
>>> "Matrix indexing (x[i] with a logical or a 2-column integer matrix i)
>>> using [ is not recommended. For extraction, x is first coerced to a
>>> matrix..."
>>> 
>>> ergo characters for a mixed mode frame.
>> 
>> Can we agree that it is most ironic that `[<-.data.frame` does not
>> impose coercion on its `x` argument with a 2 column matrix as `i`, but
>> that `[.data.frame` does? I had initially assumed that the coercion had
>> occurred at the time of assignment which would have made more sense (to
>> me, anyway).
> 

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list