Hi, I am tring to write a loop to compute this, =========================x1=c( rep(-1,4), rep(1,4) ) x2=c( rep(c(-1,-1,1,1),2) ) x3=c( rep(c(-1,1),4) ) x1*x2 x1*x3 x2*x3 ======================= suppose i have x1,x2,x3 i want to compute their ' two factor interactions', x1x2,x1x3 and x2x3, I wrote =======================for(i in 1:2){ for( j in i+1:3){ xij=c() xij=xi*xj } } =======================it did not seem to recognize xi and xj is there any suggestion? it would be wonderful if there exists a single command that i can use My ultimate aim is to find the 55 xixj s of the following data: http://n4.nabble.com/file/n1692945/test_pic.jpg test_pic.jpg Thanks. -- View this message in context: http://n4.nabble.com/R-loop-help-tp1692945p1692945.html Sent from the R help mailing list archive at Nabble.com.
Hi casperyc, Here is a suggestion: # all at once apply(combn(paste('x', 1:3, sep =""), 2), 2, function(v) get(v[1])*get(v[2]) ) # step by step thex <- paste('x', 1:3, sep ="") thex combs <- combn(thex, 2) combs apply(combs, 2, function(v) get(v[1])*get(v[2]) ) x1, x2 and x3 correspond to the variables you sent. See ?combn, ?apply, ?get and ?paste for more information. HTH, Jorge On Fri, Mar 26, 2010 at 7:01 PM, casperyc <> wrote:> > Hi, > > I am tring to write a loop to compute this, > =========================> x1=c( > rep(-1,4), > rep(1,4) > ) > > x2=c( > rep(c(-1,-1,1,1),2) > ) > > x3=c( > rep(c(-1,1),4) > ) > > x1*x2 > x1*x3 > x2*x3 > =======================> > suppose i have x1,x2,x3 > i want to compute their ' two factor interactions', x1x2,x1x3 and x2x3, > I wrote > > =======================> for(i in 1:2){ > for( j in i+1:3){ > xij=c() > xij=xi*xj > } > } > =======================> it did not seem to recognize xi and xj > > is there any suggestion? > it would be wonderful if there exists a single command that i can use > > My ultimate aim is to find the 55 xixj s of the following data: > http://n4.nabble.com/file/n1692945/test_pic.jpg test_pic.jpg > > > Thanks. > -- > View this message in context: > http://n4.nabble.com/R-loop-help-tp1692945p1692945.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]]
An aside to the main question: I don't think that i+1:3 is doing what you think it is. On 26/03/2010 23:01, casperyc wrote:> > Hi, > > I am tring to write a loop to compute this, > =========================> x1=c( > rep(-1,4), > rep(1,4) > ) > > x2=c( > rep(c(-1,-1,1,1),2) > ) > > x3=c( > rep(c(-1,1),4) > ) > > x1*x2 > x1*x3 > x2*x3 > =======================> > suppose i have x1,x2,x3 > i want to compute their ' two factor interactions', x1x2,x1x3 and x2x3, > I wrote > > =======================> for(i in 1:2){ > for( j in i+1:3){ > xij=c() > xij=xi*xj > } > } > =======================> it did not seem to recognize xi and xj > > is there any suggestion? > it would be wonderful if there exists a single command that i can use > > My ultimate aim is to find the 55 xixj s of the following data: > http://n4.nabble.com/file/n1692945/test_pic.jpg test_pic.jpg > > > Thanks.-- Patrick Burns pburns at pburns.seanet.com http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno')
Reasonably Related Threads
- how to create a substraction matrix (subtract a row of every column from the same row in other columns)
- Selecting Best Regression Equation : leaps() in R and stepwise() in S+
- Apache + Displaying images
- rbind has confusing result for custom sub-class (possible bug?)
- rbind has confusing result for custom sub-class (possible bug?)