[R] A question on operation on list

David Winsemius dwinsemius at comcast.net
Wed Jul 22 21:46:25 CEST 2009


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]])  
> %*%
> x[[i]] i.e. for each i, I want to get a 2x2 matrix

Why not use an object of class matrix?

x <- matrix(rnorm(10), nrow=5)
 > t(x) %*% x
           [,1]      [,2]
[1,]  6.621608 -1.558713
[2,] -1.558713  1.555916


> 2. Next I want to get  x[[1]] + x[[2]] +....
>
> I did following : res=vector("list"); res = sapply(x, function(i)  
> t(x[[i]])
> %*% x[[i]])
> However above syntax is not giving desired result. Any suggestion  
> please?
>
> -- 
> View this message in context: http://www.nabble.com/A-question-on-operation-on-list-tp24612796p24612796.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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list