-3

I have a table presenting the correlations between three different variables (Correlation1, Correlation2, Correlation 3) of each source (1,...,n). However, not every source reports every correlation.

Now I want to create a matrix indicating which of these correlations is given in which source. The number of rows of the indicator matrix should be equal to the total number of values given in the corr.table without NA. The indicator is always on the diagonal and set to "1" if the corresponding correlation at this position is given.

I have summarized my problem in a simple example:

enter image description here

Here, source 1 contains the first correlation, therefore the first value of the diagonal is set to "1" and all other elements are "0". The same is given in source 2. Source 3 reports all three correlations, therefore the first value in the third row will be set to "1", the second element in the fourth row is also set to "1" and the third value in fifth row is set to "1". And so on....

Do you have an idea, how to create this indicator matrix given the correlation matrix in R?

jeffrey
  • 2,026
  • 5
  • 28
  • 42
  • 3
    Please read [this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) and please update your question accordingly. What have you tried? Cheers. – Henrik Sep 11 '13 at 15:31
  • Can you please clarify the conditions to set 1 or 0 in the indicator matrix?? – Jilber Urbina Sep 11 '13 at 15:45

1 Answers1

0

This might work:

 > df <- matrix(rnorm(18),6,3)
    > df[4,1] <- NA
    > df[3,2] <- NA
    > df[6,3] <- NA

> df
            [,1]        [,2]       [,3]
[1,]  0.59299285  0.47057987  1.5362658
[2,] -1.06073361  0.03898895 -0.3732643
[3,]  0.35102152          NA  0.7484060
[4,]          NA  0.58117835 -0.4967971
[5,]  0.09878368 -0.71531458  1.4571918
[6,] -0.86293568 -0.07522243         NA

The main function is is.na. The rest: as.numeric etc. turn it into the form you want.

> idmat <-  1-matrix(as.numeric(is.na(df)),6,3)
     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    1    1    1
[3,]    1    0    1
[4,]    0    1    1
[5,]    1    1    1
[6,]    1    1    0

EDIT: As @Ferdinand points out, the following is simpler:

> idmat <- 1-is.na(df)
     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    1    1    1
[3,]    1    0    1
[4,]    0    1    1
[5,]    1    1    1
[6,]    1    1    0
harkmug
  • 2,725
  • 22
  • 26
  • 1
    Or simply `1-is.na(df)`. – Ferdinand.kraft Sep 11 '13 at 15:46
  • almost yes, but the value at your position [1,2] should be at position [2,2] and the value at position [1,3] should be at position [3,3]. For the row [3,] in your example will be only two rows in the indicator matrix: [7,1] would be 1 and [8,3] would be 1... I know my question and example was a little bit strange. but the colors should make it very simple to understand my problem. – jeffrey Sep 11 '13 at 16:08