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

David Winsemius dwinsemius at comcast.net
Sat Dec 17 23:58:24 CET 2016


> 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.


> 
> Cheers,
> Bert
> 
> 
> Bert Gunter
> 
> "The trouble with having an open mind is that people keep coming along
> and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> 
> 
> On Sat, Dec 17, 2016 at 10:49 AM, David Winsemius
> <dwinsemius at comcast.net> wrote:
>> This puzzle started with an SO posting where the questioner showed output from a dataframe that had been indexed with a matrix. The output appeared to show that numeric values had been coerced to character. Once I got a reproducible example I discovered that the print output was the problem and that the actual values had not been coerced. I've created a much smaller test case and it appears from the testing below that a matrix indexed output from a dataframe with mixed numeric and character types will be printed as character even if none of the values indexed are character:
>> 
>>> dat <- setNames( as.data.frame( matrix(1:12, ncol=4) ), LETTERS[1:4])
>>> dat
>>  A B C  D
>> 1 1 4 7 10
>> 2 2 5 8 11
>> 3 3 6 9 12
>>> dat[2,4]<-NA
>>> dat[3,3]<-NA
>>> ng <- which(is.na(dat), arr.ind=TRUE)
>>> ng
>>     row col
>> [1,]   3   3
>> [2,]   2   4
>>> dat[ng] <- 20
>>> dat[ng]
>> [1] 20 20
>> 
>> That was as expected. Now repeat the process with a dataframe of mixed types.
>> 
>>> dat[2,4]<-NA
>>> dat[3,3]<-NA
>>> dat[,1]<- "a"
>>> dat
>>  A B  C  D
>> 1 a 4  7 10
>> 2 a 5  8 NA
>> 3 a 6 NA 12
>>> dat[ng] <- 20
>>> dat[ng]
>> [1] "20" "20"
>> 
>> Quoted print output was not what I was expecting.
>> 
>> --
>> 
>> David Winsemius
>> Alameda, CA, USA
>> 
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list