2021年1月18日星期一

correlation matrix from text file

I am trying to make correlation matrix from a text files what I have. I want to get the correlation values from these files.

text file what I have

[56] "[1] \"values ""of the                                                                                                            [57] "[1] \"e"xamples                                                                                                                [58] "[1] \"dummy ""lines                                                                                              [59] "[1] \"testing"                                                                                                                       [60] "[1] \"Correlation Values\""                                                                                                           [61] "[1] \"Correlation between XXX and YYY: 0.7054 (0.0429)\""                                                                              [62] "[1] \"Correlation between XXX and ZZZ: 0.601 (0.0289)\""                                                                               [63] "[1] \"Correlation between YYY and ZZZ: 0.6434 (0.0306)\""                                                                              [64] "[1] \"Finished\""                                                                                          [65] "[1] \"testing ""linne                                                                              [66] "test"                                                                                                                                            [67] "test "again     

The matrix will look like

      XXX       YYY      ZZZ  XXX   1        0.7054    0.601  YYY   0.7054   1         0.6434  ZZZ   0.601    0.6434    1  

I understand that there is some regex technique involved, but think its too advanced for a novice like me. I can get the lines what I want from the file using the following, but still not able to workout the way to extract those numbers and put in a matrix.

mm[grep("Correlation Values", mm, value = FALSE) + c(1:3)] ## m is the above file that I loaded.  

To add the complexity to it the variables and number change in all files. Say this is the case of 4*4 matrix

[95] "[1] \"Correlation Values\""                                                                                                                                    [96] "[1] \"Correlation between XXX and YYY: 0.7054 (0.0429)\""                                                                                                       [97] "[1] \"Correlation between XXX and ZZZ: 0.601 (0.0289)\""                                                                                                        [98] "[1] \"Correlation between XXX and CCC: 0.0178 (0.0281)\""                                                                                                       [99] "[1] \"Correlation between YYY and ZZZ: 0.6434 (0.0306)\""                                                                                                      [100] "[1] \"Correlation between YYY and CCC: 0.0103 (0.0286)\""                                                                                                      [101] "[1] \"Correlation between ZZZ and CCC: 0.0174 (0.0202)\""                                                                                                      [102] "[1] \"Finished\""      
https://stackoverflow.com/questions/65777855/correlation-matrix-from-text-file January 18, 2021 at 11:53PM

没有评论:

发表评论