>I have dataset with n variables, say n =3 x y z
>the formula should be form = x+y+z+x*x +x*y+x*z +y*y + y*z+z*z
>my code has to for loop
>Is there any way to reduce two loops
> for (i in 1:n)
> for (j in i:n)
> dat= cbind(dat,dat[,i]*dat[,j])
Yes; use outer
- Why only two levels of looping for three variables?
- What have i, j and dat to do with the formula?
- Why doesnt the formula appear anywhere in the loop?