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

Rui Barradas ruipbarradas at sapo.pt
Tue May 1 21:52:50 CEST 2012


P.S.

The way the logical matrix is constructed is NOT general purpose.
Quoting myself quoting Bert,
>
> Actually, it works, as long as the logical index matrix has the same
> dimensions as the data frame.
> 
> zmat <- matrix(1:12,nr=4)
> zdf <- data.frame(zmat)
> 
> # Numeric index matrix.
> ix <- cbind(1:2,2:3)
> # Logical index matrix.
> ix2 <- row(zdf) == ix[, 1] & col(zdf) == ix[, 2]
> 

Here the number of rows in zdf is a multiple of the vectors ix[, 1] and ix[
, 2] lengths.
The recycling rules makes it work. But if the numeric index matrix has, say,
3 rows,
another way of constructing the logical one would be needed.

jx <- cbind(1:3, c(2:3, 3))
row(zdf) == jx[, 1] & col(zdf) == jx[, 2]
      [,1]  [,2]  [,3]
[1,] FALSE FALSE FALSE
[2,] FALSE FALSE FALSE
[3,] FALSE FALSE FALSE
[4,] FALSE FALSE FALSE

(Anyway, I don't believe that was the point.)

R.B.



--
View this message in context: http://r.789695.n4.nabble.com/Data-frame-vs-matrix-quirk-Hinky-error-message-tp4601254p4601558.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list