carla moreira
2011-Jun-13 13:23 UTC
[R] How to calculate the product of every two elements in two lists?
u<-c(0.1,0.2,0.3) v<-c(0.2,0.3,0.5) outer1<-outer(u,u,">=") outer2<-outer(v,v,">=") m<-nrow(outer1) j<-nrow(outer2) zz<-lapply(1:m, function(m) as.numeric(outer1[m,])) tt<-lapply(1:m, function(m) as.numeric(outer2[m,])) zz[[1]]*tt[[3]], e.g., is possible, but I want every products between two lists. Is there a way to do that? -- View this message in context: http://r.789695.n4.nabble.com/How-to-calculate-the-product-of-every-two-elements-in-two-lists-tp3593832p3593832.html Sent from the R help mailing list archive at Nabble.com.
Joshua Wiley
2011-Jun-13 14:11 UTC
[R] How to calculate the product of every two elements in two lists?
Hi, Do you mean something like this? mapply(`*`, zz, tt) [,1] [,2] [,3] [1,] 1 1 1 [2,] 0 1 1 [3,] 0 0 1 or do you want each element of zz by each element of tt (i.e., every possible 2-way combination)? Cheers, Josh On Mon, Jun 13, 2011 at 6:23 AM, carla moreira <carlamgmm at gmail.com> wrote:> > u<-c(0.1,0.2,0.3) > v<-c(0.2,0.3,0.5) > > outer1<-outer(u,u,">=") > outer2<-outer(v,v,">=") > m<-nrow(outer1) > j<-nrow(outer2) > zz<-lapply(1:m, function(m) as.numeric(outer1[m,])) > tt<-lapply(1:m, function(m) as.numeric(outer2[m,])) > > zz[[1]]*tt[[3]], e.g., is possible, but I want every products between two > lists. > > Is there a way to do that? > > -- > View this message in context: http://r.789695.n4.nabble.com/How-to-calculate-the-product-of-every-two-elements-in-two-lists-tp3593832p3593832.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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
carla moreira
2011-Jun-13 14:15 UTC
[R] How to calculate the product of every two elements in two lists?
Thank you very much. I want each element of zz by each element of tt (i.e., every possible 2-way combination). -- View this message in context: http://r.789695.n4.nabble.com/How-to-calculate-the-product-of-every-two-elements-in-two-lists-tp3593832p3593941.html Sent from the R help mailing list archive at Nabble.com.
Joshua Wiley
2011-Jun-13 14:20 UTC
[R] How to calculate the product of every two elements in two lists?
Okay, what about this: ## use expand.grid() to create a data frame of all combinations ## of the element numbers of zz and tt index <- expand.grid(seq_along(zz), seq_along(tt)) index ## use the index to select the element of zz and tt and find their product mapply(`*`, zz[index[, 1]], tt[index[, 2]]) HTH, Josh On Mon, Jun 13, 2011 at 7:12 AM, Carla Moreira <carlamgmm at gmail.com> wrote:> Thank you very much. > > I want each element of zz by each element of tt (i.e., every > possible 2-way combination). > > > Carla-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Dennis Murphy
2011-Jun-13 23:07 UTC
[R] How to calculate the product of every two elements in two lists?
Hi: Here's a slightly different approach: # Create two numeric matrices with outer(): o1 <- outer(u,u,">=") * 1L o2 <- outer(v, v, ">=") * 1L # Make a list of matrices that multiplies column i of o1 # vs. each column of o2 - use the * operator to do this # Shows what is going on at each step: lapply(seq(ncol(o1)), function(s) o1[, s] * o2) # Concatenate all product columns together into a matrix: do.call(cbind, lapply(seq(ncol(o1)), function(s) o1[, s] * o2)) HTH, Dennis On Mon, Jun 13, 2011 at 6:23 AM, carla moreira <carlamgmm at gmail.com> wrote:> > u<-c(0.1,0.2,0.3) > v<-c(0.2,0.3,0.5) > > outer1<-outer(u,u,">=") > outer2<-outer(v,v,">=") > m<-nrow(outer1) > j<-nrow(outer2) > zz<-lapply(1:m, function(m) as.numeric(outer1[m,])) > tt<-lapply(1:m, function(m) as.numeric(outer2[m,])) > > zz[[1]]*tt[[3]], e.g., is possible, but I want every products between two > lists. > > Is there a way to do that? > > -- > View this message in context: http://r.789695.n4.nabble.com/How-to-calculate-the-product-of-every-two-elements-in-two-lists-tp3593832p3593832.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. >