Quantcast

Help in creating a matrix

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Help in creating a matrix

arunkumar1111
Hi

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])
      }  
   }




Thanks in Advance
        Arun
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Help in creating a matrix

S Ellison-2
>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

But
- 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?


-----
Thanks in Advance
        Arun
--
View this message in context: http://r.789695.n4.nabble.com/Help-in-creating-a-matrix-tp4551532p4551532.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[hidden email] 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.
*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Help in creating a matrix

arunkumar1111
The formula is created sepearately and data is created for the formula and then use the modelling lm
Thanks in Advance
        Arun
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Help in creating a matrix

Rui Barradas
This post has NOT been accepted by the mailing list yet.
In reply to this post by arunkumar1111
Hello,

Would this do?

mat <- matrix(1:9, ncol=3)
poly(mat, raw=TRUE, degree=2)


It includes all linear and all 2nd degree terms.

Hope this helps,

Rui Barradas
Loading...