[R] a^c(1:3)

2010-09-07 Thread Feng Li
Dear R, I have two small questions confused me recently. Now assume I have a matrix a, like this, a - matrix(1:6, 2, 3) a [,1] [,2] [,3] [1,]135 [2,]246 I sometimes need each row of a raised to a different exponent. So I do a trick like this, a^c(2, 3) [,1]

Re: [R] a^c(1:3)

2010-09-07 Thread Dimitris Rizopoulos
^ is vectorized operator, so a^c(2,3) is essentially the same as a^rep(c(2,3), length.out = length(a)) which is c(a)^rep(c(2,3), length.out = length(a)) but put back in a matrix format (i.e., with rows and columns). Now, if you want each column in different power, you need to explicitly

Re: [R] a^c(1:3)

2010-09-07 Thread Erik Iverson
Feng, Hello, all of this behavior comes down to argument recycling. Feng Li wrote: Dear R, I have two small questions confused me recently. Now assume I have a matrix a, like this, a - matrix(1:6, 2, 3) a [,1] [,2] [,3] [1,]135 [2,]246 I sometimes need each

Re: [R] a^c(1:3)

2010-09-07 Thread David Winsemius
On Sep 7, 2010, at 12:35 PM, Feng Li wrote: Dear R, I have two small questions confused me recently. Now assume I have a matrix a, like this, a - matrix(1:6, 2, 3) a [,1] [,2] [,3] [1,]135 [2,]246 I sometimes need each row of a raised to a different

Re: [R] a^c(1:3)

2010-09-07 Thread Feng Li
Very fruitful, thanks all of you:) Feng [[alternative HTML version deleted]] __ R-help@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

Re: [R] a^c(1:3)

2010-09-07 Thread Ben Bolker
Feng Li feng.li at stat.su.se writes: Very fruitful, thanks all of you:) ?sweep may be useful as well (I don't think anyone has mentioned it yet, sorry if redundant). It handles row/column-wise operations in a way that is independent of the underlying column/row ordering or recycling