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]] +....
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.
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, 2009 at 3:18 PM, megh <megh700004@yahoo.com> wrote:
>
> 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]] +....
>
> 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@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.
>
[[alternative HTML version deleted]]
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 2 2> colSums(abind(along=0, x2))[,1] [,2] [1,] 1.768406 -1.534413 [2,] -1.534413 3.890200>-- Tony Plate megh wrote:> 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]] +.... > > 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? >
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 matrixWhy 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
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("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, 2009 at 3:18 PM, megh <megh700004 at yahoo.com>
wrote:
>
>>
>> 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]] +....
>>
>> 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.
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
>
--
View this message in context:
http://www.nabble.com/A-question-on-operation-on-list-tp24612796p24614641.html
Sent from the R help mailing list archive at Nabble.com.
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 Velez wrote: >> >> 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, 2009 at 3:18 PM, megh <megh700004 at yahoo.com> wrote: >> >>> >>> 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]] +.... >>> >>> 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. >>> >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> 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. >> >> > > -- > View this message in context: http://www.nabble.com/A-question-on-operation-on-list-tp24612796p24614641.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
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 = y = vector("list") for (i in 1:5) { x[[i]] = rnorm(2) y[[i]] = rnorm(2) } ## option 1 X <- matrix(unlist(x), nrow = 2) Y <- matrix(unlist(y), nrow = 2) C <- crossprod(X, Y) res1 <- diag(C) ## option 2 res2 <- lapply(1:5, function(i) t(x[[i]]) %*% y[[i]]) res2 <- unlist(res2) all.equal(res1, res2) # should be TRUE In this case, option 1 is quicker by a factor of 2 if that makes a difference in your application, despite doing more manipulations: foo1 <- function(x, y) { X <- matrix(unlist(x), nrow = 2) Y <- matrix(unlist(y), nrow = 2) C <- crossprod(X, Y) diag(C) } foo2 <- function(x, y) { res <- lapply(1:5, function(i) t(x[[i]]) %*% y[[i]]) unlist(res) } system.time(replicate(10000, foo1(x,y))) system.time(replicate(10000, foo2(x,y)))> system.time(replicate(10000, foo1(x,y)))user system elapsed 0.535 0.003 0.540> system.time(replicate(10000, foo2(x,y)))user system elapsed 0.994 0.001 1.022 HTH G> > Regards, > > > > Jorge Ivan Velez wrote: > > > > 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, 2009 at 3:18 PM, megh <megh700004 at yahoo.com> wrote: > > > >> > >> 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]] +.... > >> > >> 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. > >> > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > 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. > > > > >-- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Dr. Gavin Simpson [t] +44 (0)20 7679 0522 ECRC, UCL Geography, [f] +44 (0)20 7679 0565 Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/ UK. WC1E 6BT. [w] http://www.freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%