1

I am not professional with r and need your help with this question - I have a list called result with different items inside as below:

result:

$r
              0610007P14Rik 0610009B22Rik 0610009O20Rik 0610010F05Rik 0610010K14Rik 0610011F06Rik 0610012G03Rik 0610030E20Rik 0610037L13Rik
0610007P14Rik    0.00000000   -0.66234644   0.047111033    0.09782589   0.145761085   0.084414075    0.05975822    0.10952475  -0.020151257
0610009B22Rik   -0.16234644    0.00000000  -0.227292854   -0.05088201  -0.100237074   0.078595470   -0.12782382   -0.05553298   0.012588413

$p
              0610007P14Rik 0610009B22Rik 0610009O20Rik 0610010F05Rik 0610010K14Rik 0610011F06Rik 0610012G03Rik 0610030E20Rik 0610037L13Rik
0610007P14Rik     1.0000000     0.04047111     0.6405067     0.3310033     0.1459042     0.4019556  5.534329e-01     0.2760502  8.417704e-01
0610009B22Rik    0.1047111     1.0000000     0.44868459     0.6139574     0.3191458     0.4353240  2.029875e-01     0.5818857  9.007631e-01

What I want to do is to loop on this list and print pairs if result$r is > then 0.5 or < than -0.5 - and result$p < than 0.05.

So far I have been able to loop through the list by a simple loop where I can print any value at a specific position in the list but have not been able to expand it to perform what I am aiming to:

for (i in 1:length(result)){
  print(result[[i]][2])
}

So based on the example above - the output should look like this because this is the only pair that has theresult$r value less than -0.5 and result$p < than 0.05:

0610007P14Rik,0610009B22Rik

Any help is appreciated - thanks.

dput(result)

list(r = structure(c(0, -0.662346440956915, 0.0471110327396697, 
0.0978258929200013, 0.14576108466075, 0.0844140746798007, 0.0597582241031368, 
0.10952475161027, -0.0201512568819922, -0.162346440956915, 0, 
-0.0272928544838261, -0.0508820105675817, -0.100237073610376, 
0.0785954698120888, -0.127823820999628, -0.0555329766448806, 
0.0125884127823821, 0.0471110327396697, -0.0272928544838261, 
0, -0.0565079080178134, 0.13575892944611, 0.0754843375985575, 
0.086120417719783, 0.119119974969681, 0.00175356100237076, 0.0978258929200013, 
-0.0508820105675817, -0.0565079080178134, 0, 0.131775445763479, 
0.053017452395846, 0.0712198787836846, -0.0643888838089917, 0.112498034323393, 
0.14576108466075, -0.100237073610376, 0.13575892944611, 0.131775445763479, 
0, 0.00774829446850978, -0.0987186269323458, 0.0147657064131866, 
0.0300260585042811, 0.0844140746798007, 0.0785954698120888, 0.0754843375985575, 
0.053017452395846, 0.00774829446850978, 0, 0.10393741385522, 
0.0236032173803311, -0.0182926697871553, 0.0597582241031368, 
-0.127823820999628, 0.086120417719783, 0.0712198787836846, -0.0987186269323458, 
0.10393741385522, 0, -0.0287458971316651, -0.378837751523345, 
0.10952475161027, -0.0555329766448806, 0.119119974969681, -0.0643888838089917, 
0.0147657064131866, 0.0236032173803311, -0.0287458971316651, 
0, 0.137186835947971, -0.0201512568819922, 0.0125884127823821, 
0.00175356100237076, 0.112498034323393, 0.0300260585042811, -0.0182926697871553, 
-0.378837751523345, 0.137186835947971, 0), .Dim = c(9L, 9L), .Dimnames = list(
    c("0610007P14Rik", "0610009B22Rik", "0610009O20Rik", "0610010F05Rik", 
    "0610010K14Rik", "0610011F06Rik", "0610012G03Rik", "0610030E20Rik", 
    "0610037L13Rik"), c("0610007P14Rik", "0610009B22Rik", "0610009O20Rik", 
    "0610010F05Rik", "0610010K14Rik", "0610011F06Rik", "0610012G03Rik", 
    "0610030E20Rik", "0610037L13Rik"))), p = structure(c(1, 0.0404711061993476, 
0.640506733726655, 0.331003274745838, 0.145904233294019, 0.401955560275036, 
0.553432861540715, 0.276050189693768, 0.841770371470927, 0.104711061993476, 
1, 0.786845892450507, 0.613957357859433, 0.319145768537945, 0.4353239557619, 
0.202987516780359, 0.581885722125544, 0.900763057605725, 0.640506733726655, 
0.786845892450507, 1, 0.575261418533015, 0.17603176034094, 0.453788945686311, 
0.392462976283337, 0.23580507604055, 0.986141912945937, 0.331003274745838, 
0.613957357859433, 0.575261418533015, 1, 0.189217146197476, 0.599137018214154, 
0.479787539192647, 0.523038223686805, 0.263117246150375, 0.145904233294019, 
0.319145768537945, 0.17603176034094, 0.189217146197476, 1, 0.93882240484224, 
0.326580916483781, 0.88370977100432, 0.766081848734211, 0.401955560275036, 
0.4353239557619, 0.453788945686311, 0.599137018214154, 0.93882240484224, 
1, 0.3014852587724, 0.815110528089105, 0.85620041392968, 0.553432861540715, 
0.202987516780359, 0.392462976283337, 0.479787539192647, 0.326580916483781, 
0.3014852587724, 1, 0.775787682483708, 7.84039808365529e-05, 
0.276050189693768, 0.581885722125544, 0.23580507604055, 0.523038223686805, 
0.88370977100432, 0.815110528089105, 0.775787682483708, 1, 0.17147275199326, 
0.841770371470927, 0.900763057605725, 0.986141912945937, 0.263117246150375, 
0.766081848734211, 0.85620041392968, 7.84039808365529e-05, 0.17147275199326, 
1), .Dim = c(9L, 9L), .Dimnames = list(c("0610007P14Rik", "0610009B22Rik", 
"0610009O20Rik", "0610010F05Rik", "0610010K14Rik", "0610011F06Rik", 
"0610012G03Rik", "0610030E20Rik", "0610037L13Rik"), c("0610007P14Rik", 
"0610009B22Rik", "0610009O20Rik", "0610010F05Rik", "0610010K14Rik", 
"0610011F06Rik", "0610012G03Rik", "0610030E20Rik", "0610037L13Rik"
))))

2 Answers 2

3

You should not loop through the list, as the list is a fixed format and you know that there are the elements $rand $p. Instead, you should loop over the rows and columns you have:

for(row in rownames(result$p)){
  for(col in colnames(result$p)){
    if(abs(result$r[row,col])>.5 & result$p[row,col]<.05){
      print(paste(row,col,sep=", "))
    }
  }
}

The output is:

"0610009B22Rik, 0610007P14Rik"

This is the only correlation that meets your criterion (r=-.66, p=.040)

Sign up to request clarification or add additional context in comments.

2 Comments

Dear @Martin - I updated the question so now based on the examples and the dput(result) the output should look like 0610007P14Rik,0610009B22Rik - thank you for your help. I tried your previous answer and it gives structure error.
Thanks. I updated the code. There was a typo in it. Now, it works fine.
1

You can do the comparison directly if the r and p matrix are of the same dimension.

inds <- which(result$r > 0.5 | result$r < -0.5 & result$p < 0.05, arr.ind = TRUE)

cbind(row = rownames(result$p)[inds[, 1]], 
      col = colnames(result$p)[inds[, 2]])

#                row             col            
#[1,] "0610009B22Rik" "0610007P14Rik"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.