Re: [R] A question on operation on list

2009-07-23 Thread Gavin Simpson
On Wed, 2009-07-22 at 14:07 -0700, megh wrote: > Thanks for your suggestions. I need one more thing : > > x = y = vector("list") > for (i in 1:5) x[[i]] = rnorm(2); y[[i]] = rnorm(2) > > Here I want to get t(x[[i]]) %*% y[[i]] for each i. Can anyone please help > me? Two ways: set.seed(123) x

Re: [R] A question on operation on list

2009-07-22 Thread David Winsemius
On Jul 22, 2009, at 5:07 PM, megh wrote: Thanks for your suggestions. I need one more thing : x = y = vector("list") for (i in 1:5) x[[i]] = rnorm(2); y[[i]] = rnorm(2) Here I want to get t(x[[i]]) %*% y[[i]] for each i. Can anyone please help me? ?lapply Regards, Jorge Ivan Vel

Re: [R] A question on operation on list

2009-07-22 Thread megh
Thanks for your suggestions. I need one more thing : x = y = vector("list") for (i in 1:5) x[[i]] = rnorm(2); y[[i]] = rnorm(2) Here I want to get t(x[[i]]) %*% y[[i]] for each i. Can anyone please help me? Regards, Jorge Ivan Velez wrote: > > Hi megh, > Perhaps? > > # Data > x = vector("

Re: [R] A question on operation on list

2009-07-22 Thread David Winsemius
On Jul 22, 2009, at 3:18 PM, megh wrote: Hi, I have created a list object like that : x = vector("list") for (i in 1:5) x[[i]] = rnorm(2) x So now you have a list with 5 elements Now I want to do two things : 1. for each i, I want to do following matrix calculation : t(x[[i]]) %*%

Re: [R] A question on operation on list

2009-07-22 Thread Tony Plate
Here's one way: set.seed(1) x1 <- lapply(1:5, function(i) rnorm(2)) x2 <- lapply(x1, function(x) outer(x, x)) Reduce("+", x2, 0) [,1] [,2] [1,] 1.768406 -1.534413 [2,] -1.534413 3.890200 and another way using the abind package: library(abind) dim(abind(along=0, x2)) [1] 5

Re: [R] A question on operation on list

2009-07-22 Thread Jorge Ivan Velez
Hi megh, Perhaps? # Data x = vector("list") for (i in 1:5) x[[i]] = rnorm(2) # 2x2 matrices res <- lapply(x, function(a) a %*% t(a) ) res # Funcion from ?Reduce add <- function(x) Reduce("+", x) # Summing up! add(res) See ?lapply and ?Reduce for more information. HTH, Jorge On Wed, Jul 22,

[R] A question on operation on list

2009-07-22 Thread megh
Hi, I have created a list object like that : x = vector("list") for (i in 1:5) x[[i]] = rnorm(2) x Now I want to do two things : 1. for each i, I want to do following matrix calculation : t(x[[i]]) %*% x[[i]] i.e. for each i, I want to get a 2x2 matrix 2. Next I want to get x[[1]] + x[[2]] +...