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? Thanks very much in advance. -- View this message in context: http://r.789695.n4.nabble.com/Odd-subsetting-behaviour-tp2279957p2279957.html Sent from the R help mailing list archive at Nabble.com.
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
Tena koe Compare 1:3%/%2 and 1:(3%/%2) HTH ... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of math_daddy > Sent: Wednesday, 7 July 2010 5:57 a.m. > To: r-help at r-project.org > Subject: [R] Odd subsetting behaviour > > > 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? > > Thanks very much in advance. > -- > View this message in context: http://r.789695.n4.nabble.com/Odd- > subsetting-behaviour-tp2279957p2279957.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > 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.