Hi, I compute the value of xi*xj by "for" loops, this how I try:> x1 <- c(1:3) > x2 <- c(2:4)### to compute x1*x2> (paste("x", 1, sep = ""))*(paste("x", 2, sep = ""))Error in (paste("x", 1, sep = "")) * (paste("x", 2, sep = "")) : non-numeric argument to binary operator>All comments are appreciated. Thanks, cruz
markleeds at verizon.net
2009-Feb-05 03:57 UTC
[R] non-numeric argument to binary operator
hi: it's not clear to me what you're trying to do but maybe outer is what you want ? outer(x,y) it takes every value in x and pairs it with every value in y and the default operation is multiply. see details by doing ?outer. On Wed, Feb 4, 2009 at 10:36 PM, cruz wrote:> Hi, > > I compute the value of xi*xj by "for" loops, this how I try: > >> x1 <- c(1:3) >> x2 <- c(2:4) > > ### to compute x1*x2 > >> (paste("x", 1, sep = ""))*(paste("x", 2, sep = "")) > Error in (paste("x", 1, sep = "")) * (paste("x", 2, sep = "")) : > non-numeric argument to binary operator >> > > All comments are appreciated. > > Thanks, > cruz > > ______________________________________________ > 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.
Bill.Venables at csiro.au
2009-Feb-05 04:32 UTC
[R] non-numeric argument to binary operator
You are making difficulties where there are none. Consider> x1 <- 1:3 # Why c(...)? > x2 <- 2:4 > x12 <- x1*x2 # element by element product (matlab .* operator) > x12[1] 2 6 12> x12 <- outer(x1, x2) # another possibility - outer product > x12[,1] [,2] [,3] [1,] 2 3 4 [2,] 4 6 8 [3,] 6 9 12> x12 <- x1 %*% x2 # yet another possibility - inner product > x12[,1] [1,] 20>It's probably best if you start learning R by reading the introductory material and trying it out. If the going gets tough, persevere. Trying to learn R directly from R-help can get a bit tedious (on both sides). Bill Venables http://www.cmis.csiro.au/bill.venables/ -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of cruz Sent: Thursday, 5 February 2009 1:36 PM To: r-help at r-project.org Subject: [R] non-numeric argument to binary operator Hi, I compute the value of xi*xj by "for" loops, this how I try:> x1 <- c(1:3) > x2 <- c(2:4)### to compute x1*x2> (paste("x", 1, sep = ""))*(paste("x", 2, sep = ""))Error in (paste("x", 1, sep = "")) * (paste("x", 2, sep = "")) : non-numeric argument to binary operator>All comments are appreciated. Thanks, cruz ______________________________________________ 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.