Skip to content
Prev 293239 / 398502 Next

Data frame vs matrix quirk: Hinky error message?

On May 1, 2012, at 1:33 PM, Bert Gunter wrote:

            
I'm not sure _I_ would have expected '[<-.data.frame' to recognize  
that a matrix was being offered because the "[.]" formalism  without a  
comma (called "i-indexing" on the help page) would generally be  
referencing only columns (i.e. list elements). I had not realized the  
possibilitiy of offering a logical matrix to df but it does succeed as  
predicted by

?"[.data.frame"

" For replacement, a logical matrix (only) can be used to select the  
elements to be replaced in the same way as for a matrix."

So how you want to characterize documented behavior is your call. I  
would never choose the label you offered.


 > mtest <- matrix(FALSE, 4,4)
 > ix <- cbind(1:2,2:3)
 > dftest <- data.frame(mtest)
 > mtest[ix] <- TRUE
 > dftest[mtest] <- "a"
 > dftest
     X1    X2    X3    X4
1 FALSE     a FALSE FALSE
2 FALSE FALSE     a FALSE
3 FALSE FALSE FALSE FALSE
4 FALSE FALSE FALSE FALSE

The nonassignment operation still succeeds:

 > dftest[ix]
[1] "a" "a"