AP38
AP38

Reputation: 169

Calculate and place output in lower triangle matrix

I have dataset that looks like this:

A:B  A:C  A:D   B:C   B:D   C:D
 2    5   12     21    12     2
 4    6   25      2     1     5
10   21   89      3     3     8

I would like to calculate the mean of each column and place the output in a lower triangular matrix such as:

    A     B     C     D
A  NA
B  5.33   NA
C  11     8.6   NA
D  42     4.3   15    NA

I am trying the following code, but I'm not there yet.

result.matrix <- matrix(nrow = 4, ncol = 4)
for (i in 1:length(test)) {
  for(j in 1:length(test)) {
    result.matrix[i,j] <- cor(mean(as.numeric(test[,i])), mean(as.numeric(test[,j])), use = "na.or.complete") }}

Any help would be appreciated!

Upvotes: 1

Views: 54

Answers (1)

AP38
AP38

Reputation: 169

answer by @user20650

m[lower.tri(m)] <- colMeans(d), where m is matrix of zeros of correct dimensions

Upvotes: 1

Related Questions