[R] Data frame vs matrix quirk: Hinky error message?

(Ted Harding) Ted.Harding at wlandres.net
Tue May 1 20:03:19 CEST 2012


On 01-May-2012 17:33:23 Bert Gunter wrote:
> AdvisoRs:
> 
> Is the following a bug, feature, hinky error message, or dumb Bert?
> 
>   mtest <- matrix(1:12,nr=4)
>   dftest <- data.frame(mtest)
>   ix <- cbind(1:2,2:3)
>   mtest[ix] <- NA
>   mtest
>      [,1] [,2] [,3]
> [1,]    1   NA    9
> [2,]    2    6   NA
> [3,]    3    7   11
> [4,]    4    8   12
> 
>## But ...
>   dftest[ix] <- NA
> Error in `[<-.data.frame`(`*tmp*`, ix, value = NA) :
>   only logical matrix subscripts are allowed in replacement
> 
> Obviously, I was expecting matrix indexing for replacement to
> work similarly in both cases; however, I can see why it would
> be problematic for data frames (mixed types), but was a bit
> nonplussed by the error message, which seems hinky to me.
> 
> Cheers,
> Bert

Also interesting is that, prior to the substitution commands,

  mtest[ix]
  # [1]  5 10
  dftest[ix]
  # [1]  5 10

both as one would expect on Bert's "naive" gounds (which, I confess,
I also share[d]).

Ted.

-------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at wlandres.net>
Date: 01-May-2012  Time: 19:03:14
This message was sent by XFMail



More information about the R-help mailing list