Answers.Body: <p>You can use <code>mapply()</code>. <code>ta</code> and <code>tb</code> being transposed data frames of your <code>A</code> and <code>B</code> data frames respectively:</p>
  <pre><code>&gt; mapply(cor, ta, tb)[mapply(cor, ta, tb) &gt; 0.5]
  
        G_3       G_5       G_9      G_10      G_11      G_15      G_20      G_23      G_25      G_26      G_33      G_40      G_43      G_48      G_57      G_60 
  0.5346591 0.8066507 0.8379777 0.6752681 0.7221359 0.5285787 0.7333045 0.5627962 0.6533379 0.7256878 0.5996492 0.6486557 0.5108215 0.7386332 0.6596823 0.6919915 
       G_63      G_72      G_76      G_80      G_81      G_90      G_97      G_98      G_99 
  0.5589583 0.8391917 0.7608801 0.8003665 0.6364557 0.5030968 0.7298439 0.5693024 0.5709411 
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-08T12:43:48.200Z
Answers.Text: However, it returns NAs in my studio! 
   tb = t(B); ta = t(A)mapply(cor, ta, tb)[mapply(cor, ta, tb) > 0.5]
Answers.UserDisplayName: data
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-08T12:46:53.433Z
Answers.Text: It must be something to do with `mapply()` treating matrices and data frames differently, not sure. `t()` outputs matrices not data frames so you can do this: `tb = as.data.frame(t(B))`.
Answers.UserDisplayName: haci
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-08T12:01:43.547Z
Answers.LastActivityDate: 2020-10-08T12:01:43.547Z
Answers.UserDisplayName: haci
Body: <p>I am stuck on how to do correlation for two independent data sets with common row and column names.
  A and B are datasets that contain as many rows as genes and as many columns as samples.
  The rows in A and B represent a common set of genes but measured in two different tissues.
  The columns represent measurements in the same 5 samples in both A and B.
  I want to do a correlation between the set of genes in A and B. This is to see if the same genes in both tissues are correlated or not.
  Since the matrix would be big in my actual data, I only want to retain a correlation coefficient higher than 0.5.</p>
  <p>Here I simulate the data set.</p>
  <pre><code>set.seed(1)
  A &lt;- data.frame(rnorm(100), 
                  rnorm(100),
                  rnorm(100), 
                  rnorm(100),
                  rnorm(100))
  row.names(A) &lt;- paste0(&quot;G_&quot;, 1:100)
  colnames(A) &lt;- paste0(&quot;M_&quot;, 1:5)
  
  set.seed(42)  
  B &lt;- data.frame(rnorm(100), 
                  rnorm(100),
                  rnorm(100), 
                  rnorm(100),
                  rnorm(100))
  row.names(B) &lt;- paste0(&quot;G_&quot;, 1:100)
  colnames(B) &lt;- paste0(&quot;I_&quot;, 1:5)
  </code></pre>
  <p>Thank you!</p>
  
Tags: <r><gene><gene-expression><correlation>
Title: gene-gene correlation from two different Tissues
ThreadText: # gene-gene correlation from two different Tissues
  
  (Asked by: username_0 on 2020-10-08)
  
  I am stuck on how to do correlation for two independent data sets with common row and column names.
  A and B are datasets that contain as many rows as genes and as many columns as samples.
  The rows in A and B represent a common set of genes but measured in two different tissues.
  The columns represent measurements in the same 5 samples in both A and B.
  I want to do a correlation between the set of genes in A and B. This is to see if the same genes in both tissues are correlated or not.
  Since the matrix would be big in my actual data, I only want to retain a correlation coefficient higher than 0.5.
  
  Here I simulate the data set.
  
  ```
  set.seed(1)
  A <- data.frame(rnorm(100), 
                  rnorm(100),
                  rnorm(100), 
                  rnorm(100),
                  rnorm(100))
  row.names(A) <- paste0("G_", 1:100)
  colnames(A) <- paste0("M_", 1:5)
  
  set.seed(42)  
  B <- data.frame(rnorm(100), 
                  rnorm(100),
                  rnorm(100), 
                  rnorm(100),
                  rnorm(100))
  row.names(B) <- paste0("G_", 1:100)
  colnames(B) <- paste0("I_", 1:5)
  
  ```
  
  Thank you!
  
  **Tags:** r, gene, gene-expression, correlation
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-10-08. Score: 1 | Accepted answer
  
  You can use `mapply()`. `ta` and `tb` being transposed data frames of your `A` and `B` data frames respectively:
  
  ```
  > mapply(cor, ta, tb)[mapply(cor, ta, tb) > 0.5]
  
        G_3       G_5       G_9      G_10      G_11      G_15      G_20      G_23      G_25      G_26      G_33      G_40      G_43      G_48      G_57      G_60 
  0.5346591 0.8066507 0.8379777 0.6752681 0.7221359 0.5285787 0.7333045 0.5627962 0.6533379 0.7256878 0.5996492 0.6486557 0.5108215 0.7386332 0.6596823 0.6919915 
       G_63      G_72      G_76      G_80      G_81      G_90      G_97      G_98      G_99 
  0.5589583 0.8391917 0.7608801 0.8003665 0.6364557 0.5030968 0.7298439 0.5693024 0.5709411 
  
  ```
  
  > **Comments:**
  > **username_0**: However, it returns NAs in my studio!   tb = t(B); ta = t(A)mapply(cor, ta, tb)[mapply(cor, ta, tb) > 0.5]  
  > **username_1**: It must be something to do with `mapply()` treating matrices and data frames differently, not sure. `t()` outputs matrices not data frames so you can do this: `tb = as.data.frame(t(B))`.  
  
  ---
  
  