Re: [R] R loop help
Hi there, That's exactly what I want. I have checked ?combn out, but I could get the following, suppose that I want ALL possible combinations of them, as this == apply( combn(paste('x', 1:4, sep =), 2), 2, function(v) get(v[1])*get(v[2]) ), apply( combn(paste('x', 1:4, sep =), 3), 2, function(v) get(v[1])*get(v[2])*get(v[3]) ), apply( combn(paste('x', 1:4, sep =), 4), 2, function(v) get(v[1])*get(v[2])*get(v[3])*get(v[4]) ) ) == combn(paste('x', 1:4, sep =), 2) lists all the '2' factor combinations, combn(paste('x', 1:4, sep =),3) lists all the '3' factor combinations, ect, I have tried to use combn(paste('x', 1:4, sep =), c(2,3,4)) to get all possible combinations, but didnt work how should I proceed? Thanks! casper -- View this message in context: http://n4.nabble.com/R-loop-help-tp1692945p1753626.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.
Re: [R] R loop help
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 pbu...@pburns.seanet.com http://www.burns-stat.com (home of 'Some hints for the R beginner' and 'The R Inferno') __ 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.
[R] R loop help
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.
Re: [R] R loop help
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]] __ 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.