[R] Odd subsetting behaviour

Erik Iverson eriki at ccbr.umn.edu
Tue Jul 6 22:58:33 CEST 2010



math_daddy wrote:
> Hello. I've observed some odd behaviour. Most likely, it is already known and
> explained somewhere, but I can't find an explanation anywhere, so I would
> appreciate being pointed in the right direction.
> 
> The issue with the following code is self explanatory:
> 
>> mat <- matrix(c(c(1,1,1),c(2,2,2)),nrow=3)
>> mat[,c(1:3%/%2)]
>      [,1] [,2]
> [1,]    1    1
> [2,]    1    1
> [3,]    1    1
>> n <- 3%/%2
>> mat[,c(1:n)]
> [1] 1 1 1
> 
> What I want to know is, why does it give the first column twice when 3%/%2
> is computed within the subset call, and just the first row when it is
> computed outside?
> 

Hint:

Look at

c(1:3 %/% 2)

It's an Order of Operations Thing.

See ?Syntax



More information about the R-help mailing list