"^" 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
use rep(), e.g.,
a^rep(c(2,3,4), each = nrow(a))
I hope it helps.
Best,
Dimitris
On 9/7/2010 6: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,] 1 3 5
[2,] 2 4 6
I sometimes need each row of "a" raised to a different exponent. So I do a
trick like this,
a^c(2, 3)
[,1] [,2] [,3]
[1,] 1 9 25
[2,] 8 64 216
My first question is that if it is possible to do this trick column wise?
Just out of curiosity, of course I know there are other ways of doing this.
And the second question is why I get such result when I put another element
in the exponent part like this,
a^c(2, 3, 4)
[,1] [,2] [,3]
[1,] 1 81 125
[2,] 8 16 1296
BTW, I have a 64bit R version (2.11) for Linux. Any advice would be
appreciated.
Feng
--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center
Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
______________________________________________
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 provide commented, minimal, self-contained, reproducible code.