[R] matrix manipulations

Spencer Graves spencer.graves at pdf.com
Tue Jul 15 20:09:18 CEST 2003


What about the following:

 > x <- array(1:15, dim=c(5,3))
 > k <- dim(x)[2]
 > (P.k <- outer(rep(1,k), rep(1,k))-diag(k))
      [,1] [,2] [,3]
[1,]    0    1    1
[2,]    1    0    1
[3,]    1    1    0
 > (x.1 <- (x %*%P.k))
      [,1] [,2] [,3]
[1,]   17   12    7
[2,]   19   14    9
[3,]   21   16   11
[4,]   23   18   13
[5,]   25   20   15
 > cor(x, x.1)
      [,1] [,2] [,3]
[1,]    1    1    1
[2,]    1    1    1
[3,]    1    1    1

This has the right pieces and the correct answer in this case and 
presumably in others.

hope this helps.  spencer graves

apjaworski at mmm.com wrote:
> David,
> 
> I am not sure if it can be done in a vectorized form, but this should work
> 
> y <- NULL
> for(i in 1:ncol(x)) y <- c(y, cor(x[,i],apply(x[,-i],1,sum)))
> 
> Cheers,
> 
> Andy
> 
> __________________________________
> Andy Jaworski
> Engineering Systems Technology Center
> 3M Center, 518-1-01
> St. Paul, MN 55144-1000
> -----
> E-mail: apjaworski at mmm.com
> Tel:  (651) 733-6092
> Fax:  (651) 736-3122
> 
> 
> |---------+-------------------------------->
> |         |           "David Andel"        |
> |         |           <andel at ifi.unizh.ch> |
> |         |           Sent by:             |
> |         |           r-help-bounces at stat.m|
> |         |           ath.ethz.ch          |
> |         |                                |
> |         |                                |
> |         |           07/15/2003 11:51     |
> |         |                                |
> |---------+-------------------------------->
>   >-----------------------------------------------------------------------------------------------------------------------------|
>   |                                                                                                                             |
>   |      To:       r-help at stat.math.ethz.ch                                                                                     |
>   |      cc:                                                                                                                    |
>   |      Subject:  [R] matrix manipulations                                                                                     |
>   >-----------------------------------------------------------------------------------------------------------------------------|
> 
> 
> 
> 
> Hi
> 
> cor(x,apply(x,1,sum)) gives me the correlations of each column with the
> sums of each row (correct me if I'm wrong, please).
> 
> What I need are the correlations of each column with the sums of each row
> except the entry in the given column. It seems that for any one column i I
> get it by doing:
> 
> cor(x[,i],apply(x[,-i],1,sum))
> 
> But I struggle to get it for all the columns. I was trying things like:
> 
> for(i in 1:ncol(x)) cor(x[,i],apply(x[,-i],1,sum))
> 
> which doesn't generate any output at all, and
> 
> 
>>rbind(for(i in 1:ncol(x)) cor(x[,i],apply(x[,-i],1,sum)))
> 
>           [,1]
> [1,] 0.1880237
> 
> outputs just the result of the very last column.
> 
> I know that it shouldn't be necessary to use for(), but I couldn't figure
> out a way how to do the task using e.g. apply().
> 
> How do you get the results of all columns?
> 
> Thank you,
> David
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list