Skip to content
Prev 396615 / 398502 Next

please help generate a square correlation matrix

Hi Rui,

You are always very helpful!! Thank you,

I just modified your R codes to remove a row with zero values in both column pair as below for my real data.

Ding

dat<-gene22mut.coded
r <- P <- matrix(NA, nrow = 22L, ncol = 22L,
                 dimnames = list(names(dat), names(dat)))

for(i in 1:22) {
  #i=1
  x <- dat[[i]]
  for(j in (1:22)) {
    #j=2
    if(i == j) {
      # there's nothing to test, assign correlation 1
      r[i, j] <- 1
    } else {
      tmp <-cbind(x,dat[[j]])
      row0 <-rowSums(tmp)
      tem2 <-tmp[row0!=0,]
      tmp3 <- cor.test(tem2[,1],tem2[,2])
      r[i, j] <- tmp3$estimate
      P[i, j] <- tmp3$p.value
    }
  }
}
r<-as.data.frame(r)
P<-as.data.frame(P)

From: R-help <r-help-bounces at r-project.org> On Behalf Of Yuan Chun Ding via R-help
Sent: Thursday, July 25, 2024 11:26 AM
To: Rui Barradas <ruipbarradas at sapo.pt>; r-help at r-project.org
Subject: Re: [R] please help generate a square correlation matrix

HI Rui, Thank you for the help! You did not remove a row if zero values exist in both column pair, right? Ding From: Rui Barradas <ruipbarradas@?sapo.?pt> Sent: Thursday, July 25, 2024 11:?15 AM To: Yuan Chun Ding <ycding@?coh.?org>;


HI Rui,



Thank you for the  help!



You did not remove a row if zero values exist in both column pair, right?



Ding



From: Rui Barradas <ruipbarradas at sapo.pt<mailto:ruipbarradas at sapo.pt>>

Sent: Thursday, July 25, 2024 11:15 AM

To: Yuan Chun Ding <ycding at coh.org<mailto:ycding at coh.org>>; r-help at r-project.org<mailto:r-help at r-project.org>

Subject: Re: [R] please help generate a square correlation matrix



?s 17:?39 de 25/07/2024, Yuan Chun Ding via R-help escreveu: > Hi R users, > > I generated a square correlation matrix for the dat dataframe below; > dat<-data.?frame(g1=c(1,0,0,1,1,1,0,0,0), > g2=c(0,1,0,1,0,1,1,0,0), > g3=c(1,1,0,0,0,1,0,0,0),





?s 17:39 de 25/07/2024, Yuan Chun Ding via R-help escreveu:

        

        

            
        
<https://urldefense.com/v3/__https:/stat.ethz.ch/mailman/listinfo/r-help__;!!Fou38LsQmgU!tyykZkQmOKcwoWXEpV2ohbnr02thhHMabAcYLL_-7dteKHAabK-eo4rGDnwgSFjniAy8SO00L6Hb8338TBM$%3chttps:/urldefense.com/v3/__https:/stat.ethz.ch/mailman/listinfo/r-help__;!!Fou38LsQmgU!tyykZkQmOKcwoWXEpV2ohbnr02thhHMabAcYLL_-7dteKHAabK-eo4rGDnwgSFjniAy8SO00L6Hb8338TBM$%3e%3e>
<https://urldefense.com/v3/__http:/www.R-project.org/posting-guide.html__;!!Fou38LsQmgU!tyykZkQmOKcwoWXEpV2ohbnr02thhHMabAcYLL_-7dteKHAabK-eo4rGDnwgSFjniAy8SO00L6Hb880tLw0$%3chttps:/urldefense.com/v3/__http:/www.R-project.org/posting-guide.html__;!!Fou38LsQmgU!tyykZkQmOKcwoWXEpV2ohbnr02thhHMabAcYLL_-7dteKHAabK-eo4rGDnwgSFjniAy8SO00L6Hb880tLw0$%3e%3e>
Hello,







You are complicating the code, there's no need for as.symbol/eval, the



column numbers do exactly the same.







# create the two results matrices beforehand



r <- P <- matrix(NA, nrow = 4L, ncol = 4L, dimnames = list(names(dat),



names(dat)))







for(i in 1:4) {



   x <- dat[[i]]



   for(j in (1:4)) {



     if(i == j) {



       # there's nothing to test, assign correlation 1



       r[i, j] <- 1



     } else {



       tmp <- cor.test(x, dat[[j]])



       r[i, j] <- tmp$estimate



       P[i, j] <- tmp$p.value



     }



   }



}







# these two results are equal up to floating-point precision



dat.rcorr$r



#>           g1        g2        g3        g4



#> g1 1.0000000 0.1000000 0.3162278 0.1581139



#> g2 0.1000000 1.0000000 0.3162278 0.6324555



#> g3 0.3162278 0.3162278 1.0000000 0.0000000



#> g4 0.1581139 0.6324555 0.0000000 1.0000000



r



#>           g1        g2           g3           g4



#> g1 1.0000000 0.1000000 3.162278e-01 1.581139e-01



#> g2 0.1000000 1.0000000 3.162278e-01 6.324555e-01



#> g3 0.3162278 0.3162278 1.000000e+00 1.355253e-20



#> g4 0.1581139 0.6324555 1.355253e-20 1.000000e+00







# these two results are equal up to floating-point precision



dat.rcorr$P



#>           g1         g2        g3         g4



#> g1        NA 0.79797170 0.4070838 0.68452834



#> g2 0.7979717         NA 0.4070838 0.06758329



#> g3 0.4070838 0.40708382        NA 1.00000000



#> g4 0.6845283 0.06758329 1.0000000         NA



P



#>           g1         g2        g3         g4



#> g1        NA 0.79797170 0.4070838 0.68452834



#> g2 0.7979717         NA 0.4070838 0.06758329



#> g3 0.4070838 0.40708382        NA 1.00000000



#> g4 0.6845283 0.06758329 1.0000000         NA











You can put these two results in a list, like Hmisc::rcorr does.







lst_rcorr <- list(r = r, P = P)











Hope this helps,







Rui Barradas



















--



Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus.



https://urldefense.com/v3/__http://www.avg.com__;!!Fou38LsQmgU!tyykZkQmOKcwoWXEpV2ohbnr02thhHMabAcYLL_-7dteKHAabK-eo4rGDnwgSFjniAy8SO00L6HbloMCQMI$<https://urldefense.com/v3/__http:/www.avg.com__;!!Fou38LsQmgU!tyykZkQmOKcwoWXEpV2ohbnr02thhHMabAcYLL_-7dteKHAabK-eo4rGDnwgSFjniAy8SO00L6HbloMCQMI$><https://urldefense.com/v3/__http:/www.avg.com__;!!Fou38LsQmgU!tyykZkQmOKcwoWXEpV2ohbnr02thhHMabAcYLL_-7dteKHAabK-eo4rGDnwgSFjniAy8SO00L6HbloMCQMI$%3chttps:/urldefense.com/v3/__http:/www.avg.com__;!!Fou38LsQmgU!tyykZkQmOKcwoWXEpV2ohbnr02thhHMabAcYLL_-7dteKHAabK-eo4rGDnwgSFjniAy8SO00L6HbloMCQMI$%3e%09%5b%5balternative>

 <https://urldefense.com/v3/__http:/www.avg.com__;!!Fou38LsQmgU!tyykZkQmOKcwoWXEpV2ohbnr02thhHMabAcYLL_-7dteKHAabK-eo4rGDnwgSFjniAy8SO00L6HbloMCQMI$%3chttps:/urldefense.com/v3/__http:/www.avg.com__;!!Fou38LsQmgU!tyykZkQmOKcwoWXEpV2ohbnr02thhHMabAcYLL_-7dteKHAabK-eo4rGDnwgSFjniAy8SO00L6HbloMCQMI$%3e%09%5b%5balternative>

               [[alternative<https://urldefense.com/v3/__http:/www.avg.com__;!!Fou38LsQmgU!tyykZkQmOKcwoWXEpV2ohbnr02thhHMabAcYLL_-7dteKHAabK-eo4rGDnwgSFjniAy8SO00L6HbloMCQMI$%3chttps:/urldefense.com/v3/__http:/www.avg.com__;!!Fou38LsQmgU!tyykZkQmOKcwoWXEpV2ohbnr02thhHMabAcYLL_-7dteKHAabK-eo4rGDnwgSFjniAy8SO00L6HbloMCQMI$%3e%09%5b%5balternative>HTML version deleted]]



______________________________________________

R-help at r-project.org<mailto:R-help at r-project.org> mailing list -- To UNSUBSCRIBE and more, see

https://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-help__;!!Fou38LsQmgU!s54ahZtZNAIyaIGV3C2p8lhXpYlHksC6XvFKZltf6g3ElJHOO3I1MYFecLQ4QeMO3MpP3qXz-YrhdUE$<https://urldefense.com/v3/__https:/stat.ethz.ch/mailman/listinfo/r-help__;!!Fou38LsQmgU!s54ahZtZNAIyaIGV3C2p8lhXpYlHksC6XvFKZltf6g3ElJHOO3I1MYFecLQ4QeMO3MpP3qXz-YrhdUE$>

PLEASE do read the posting guide https://urldefense.com/v3/__http://www.R-project.org/posting-guide.html__;!!Fou38LsQmgU!s54ahZtZNAIyaIGV3C2p8lhXpYlHksC6XvFKZltf6g3ElJHOO3I1MYFecLQ4QeMO3MpP3qXzNRZxc6s$<https://urldefense.com/v3/__http:/www.R-project.org/posting-guide.html__;!!Fou38LsQmgU!s54ahZtZNAIyaIGV3C2p8lhXpYlHksC6XvFKZltf6g3ElJHOO3I1MYFecLQ4QeMO3MpP3qXzNRZxc6s$>

and provide commented, minimal, self-contained, reproducible code.
Message-ID: <MN2PR02MB69117C33C32C453380AED677D4AB2@MN2PR02MB6911.namprd02.prod.outlook.com>
In-Reply-To: <MN2PR02MB69114D44E886F65F6254CEA4D4AB2@MN2PR02MB6911.namprd02.prod.outlook.com>