vendredi 29 mars 2019

Looping comparing two values in R

While looping over a partially filled correlation matrix (it has been filtered beforehand), I want to compare the variances for the two variables within the loop and keep the variable with the highest variance in a vector.

This is a capture of the correlation matrix:

enter image description here

Variances have been calculated in a separate dataframe (var) and the order of the variances coincides with the order of the variables in the correlation matrix.

enter image description here

The snippet of code that does not work is as follows:

vec <- c()

for (i in ncol(mcor)) {
  for (j in nrow(mcor)) {
    if (is.na(mcor[i,j])) {
      j = j+1      
    } else {
      if (var[j,2] > var[i,2]) {
        vec <- c(vec, var[j,2])
      } else {
        vec <- c(vec, var[i,2])
      }
    }
  }
}

Aucun commentaire:

Enregistrer un commentaire