Pairwise Correlation Table

前端 未结 4 1510
忘掉有多难
忘掉有多难 2020-12-30 04:59

I\'m new to R, so I apologize if this is a straightforward question, however I\'ve done quite a bit of searching this evening and can\'t seem to figure it out. I\'ve got a d

4条回答
  •  暖寄归人
    2020-12-30 05:43

    Here is something that I just made, I stumbled on this post because I was looking for a way to take every pair of variables, and get a tidy nX3 dataframe. Column 1 is a variable, Column 2 is a variable, and Column 3 and 4 are their absolute value and true correlation. Just pass the function a dataframe of numeric and integer values.

      pairwiseCor <- function(dataframe){
      pairs <- combn(names(dataframe), 2, simplify=FALSE)
      df <- data.frame(Vairable1=rep(0,length(pairs)), Variable2=rep(0,length(pairs)), 
                       AbsCor=rep(0,length(pairs)), Cor=rep(0,length(pairs)))
      for(i in 1:length(pairs)){
        df[i,1] <- pairs[[i]][1]
        df[i,2] <- pairs[[i]][2]
        df[i,3] <- round(abs(cor(dataframe[,pairs[[i]][1]], dataframe[,pairs[[i]][2]])),4)
        df[i,4] <- round(cor(dataframe[,pairs[[i]][1]], dataframe[,pairs[[i]][2]]),4)
      }
      pairwiseCorDF <- df
      pairwiseCorDF <- pairwiseCorDF[order(pairwiseCorDF$AbsCor, decreasing=TRUE),]
      row.names(pairwiseCorDF) <- 1:length(pairs)
      pairwiseCorDF <<- pairwiseCorDF
      pairwiseCorDF
      }
    

    This is what the output is:

     > head(pairwiseCorDF)
                 Vairable1        Variable2 AbsCor     Cor
        1        roll_belt     accel_belt_z 0.9920 -0.9920
        2 gyros_dumbbell_x gyros_dumbbell_z 0.9839 -0.9839
        3        roll_belt total_accel_belt 0.9811  0.9811
        4 total_accel_belt     accel_belt_z 0.9752 -0.9752
        5       pitch_belt     accel_belt_x 0.9658 -0.9658
        6 gyros_dumbbell_z  gyros_forearm_z 0.9491  0.9491
    

提交回复
热议问题