[R] Data.Frame Multiplication

ggrothendieck@yifan.net ggrothendieck at yifan.net
Thu Apr 18 16:30:19 CEST 2002


The code below takes a matrix x and converts it to a matrix
whose columns are the pairwise columns of x.  If your data comes
in a data frame,df, you can always convert it to a matrix like this:
x <- as.matrix(df)

In the code below, 
zz is a matrix whose i,j-th element contains the product
of column i and column j.  Extract the upper triangular
portion of this matrix and reshape that into a matrix.  (You can
convert that to a data frame if you prefer.)

zz <- apply(outer(t(x),x),c(1,4),function(x)list(diag(x)))
matrix(unlist(zz[upper.tri(zz)]),nrow=nrow(x))

You might want to test this out a bit since the
construction is a bit tricky and I  have not extensively
tested it.

On 18 Apr 2002 at 13:03, Christian Schulz wrote:

> Have got  any  R-proffessional  a starting point for me
> how i can write me a function which multiply every column with
> every other column in the data.frame - indenpendent from the dim's .
> 
> Thanks in advance
> regards,Christian

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list