Yongwan Chun
2007-Sep-03 06:36 UTC
[R] element wise opertation between a vector and a list
I want to try to get a result of element wise addition between a vector and a list. It can be done with "for statement." In order to reducing computing time, I have tried to avoid "for state." If anybody give me an idea, I would apprecite it much. for example, with a & b as below lines, a<- list(c(1,3),c(1,2),c(2,3)) b<-c(10,20,30) I would like to have a list (like "d") or a vector (like "e") as below. d<-list(c((1+10),(3+10)),c((1+20),(2+20)),c((2+30),(3+30))) e<- c((1+10)+(3+10),(1+20)+(2+20),(2+30)+(3+30)) Thanks, Yongwan Chun
Petr PIKAL
2007-Sep-03 09:32 UTC
[R] Odp: element wise opertation between a vector and a list
Hi mapply("+", a, b) Regards Petr r-help-bounces at stat.math.ethz.ch napsal dne 03.09.2007 08:36:10:> I want to try to get a result of element wise addition between a > vector and a list. It can be done with "for statement." In order to > reducing computing time, I have tried to avoid "for state." If anybody > give me an idea, I would apprecite it much. > > for example, with a & b as below lines, > > a<- list(c(1,3),c(1,2),c(2,3)) > b<-c(10,20,30) > > I would like to have a list (like "d") or a vector (like "e") as below. > > d<-list(c((1+10),(3+10)),c((1+20),(2+20)),c((2+30),(3+30))) > e<- c((1+10)+(3+10),(1+20)+(2+20),(2+30)+(3+30)) > > Thanks, > > > Yongwan Chun > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Moshe Olshansky
2007-Sep-03 23:23 UTC
[R] element wise opertation between a vector and a list
Hi, Getting your e is not a problem:> a<- list(c(1,3),c(1,2),c(2,3)) > b<-c(10,20,30) > aa<-matrix(unlist(a),nrow=2) > ee<-aa+rbind(b,b) > e<-apply(ee,2,"sum") > e[1] 24 43 65>But this will not work if different list members have different number of elements. --- Yongwan Chun <ywchun at gmail.com> wrote:> I want to try to get a result of element wise > addition between a > vector and a list. It can be done with "for > statement." In order to > reducing computing time, I have tried to avoid "for > state." If anybody > give me an idea, I would apprecite it much. > > for example, with a & b as below lines, > > a<- list(c(1,3),c(1,2),c(2,3)) > b<-c(10,20,30) > > I would like to have a list (like "d") or a vector > (like "e") as below. > >d<-list(c((1+10),(3+10)),c((1+20),(2+20)),c((2+30),(3+30)))> e<- c((1+10)+(3+10),(1+20)+(2+20),(2+30)+(3+30)) > > Thanks, > > > Yongwan Chun > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
> ?mapply > mapply('+', a, b, SIMPLIFY=FALSE) > colSums(mapply('+', a, b))Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Yongwan Chun > Sent: Monday, September 03, 2007 12:36 AM > To: r-help at stat.math.ethz.ch > Subject: [R] element wise opertation between a vector and a list > > I want to try to get a result of element wise addition > between a vector and a list. It can be done with "for > statement." In order to reducing computing time, I have tried > to avoid "for state." If anybody give me an idea, I would > apprecite it much. > > for example, with a & b as below lines, > > a<- list(c(1,3),c(1,2),c(2,3)) > b<-c(10,20,30) > > I would like to have a list (like "d") or a vector (like "e") > as below. > > d<-list(c((1+10),(3+10)),c((1+20),(2+20)),c((2+30),(3+30))) > e<- c((1+10)+(3+10),(1+20)+(2+20),(2+30)+(3+30)) > > Thanks, > > > Yongwan Chun > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >