<p>Starting from the definition of <code>t</code>:</p>

<pre><code>...
&gt; t &lt;- sapply(seq(ncol(t(biomarker))), function(x) f(t(biomarker)[,x], t(immune)[,x]))
</code></pre>

<p>Set the column names to the gene names from one or the other of your conditions:</p>

<pre><code>&gt; colnames(t) &lt;- rownames(immune)
</code></pre>

<p>Set the cutoff:</p>

<pre><code>&gt; cutoff &lt;- 0.05
</code></pre>

<p>Filter for rows which meet the p-value cutoff criterium, and get their column names (gene names):</p>

<pre><code>&gt; colnames(t)[which(unlist(lapply(t["pval",], function(x) x &lt; cutoff)))]
[1] "ABCC6" 
...
</code></pre>

<p>Only one gene name is reported here, using your demo datasets. Adjust your cutoff and criterium logic, as needed. For example, I filter here for gene names where the p-value is <em>less</em> than the cutoff; however, from your question, you may be asking for the inverse, for whatever reason, so you could change <code>&lt;</code> to <code>&gt;=</code>.</p>

CC BY-SA 4.0
2019-01-21T22:19:54.350Z
I might suggest changing the variable name `t` to something else, as `t()` takes the transpose of the argument, if of matrix or data frame type. Using non-ambiguous variable names will make it easier for you to make changes or fix problems down the road.
Alex Reynolds
CC BY-SA 4.0
2019-01-21T19:43:39.003Z
2019-01-22T23:19:34.137Z
2019-01-22T23:19:34.137Z
Alex Reynolds
<p>From your example we are comparing samples not genes, I think we need to run the test row wise, something like this:</p>

<pre><code>pvalueGenes &lt;- sapply(colnames(biomarker),
                      function(i) t.test(biomarker[i, ],
                                         immune[i, ], paired = TRUE)$p.value)

names(pvalueGenes)[ pvalueGenes &lt; 0.05 ]
</code></pre>

CC BY-SA 4.0
2019-01-21T13:02:29.940Z
Thank you, I added output of what you suggested in my post
Zizogolu
CC BY-SA 4.0
2019-01-21T13:18:47.807Z
Sorry, I have 2 matched matrices with matched samples and genes I want to find genes with p-value < 0.05 between 2 matrices but I could not do that
Zizogolu
CC BY-SA 4.0
2019-01-21T16:58:56.603Z
@FereshTeh forget your function `f`, there is no need, just run the commands in my answer. And check what you see in `pvalueGenes` object.
zx8754
CC BY-SA 4.0
2019-01-21T17:05:27.290Z
Thank you but your code itself giving error please have a look at error I added to my post
Zizogolu
CC BY-SA 4.0
2019-01-21T19:12:32.000Z
@FereshTeh The reason for error is statistics, read about it [more here](https://stats.stackexchange.com/q/112972/6454). Suggestion, when you get an error try to use the search, 99% of the time the error has been searched before and you will find the solution/explanation.
zx8754
CC BY-SA 4.0
2019-01-22T20:41:31.793Z
@zx8754, although `names(x)[ x < 0.05 ]` works, would it not be better to use `names(x[ x < 0.05 ])`, so each layer is a logical unit?
Ram RS
CC BY-SA 4.0
2019-01-23T11:16:20.820Z
@RamRS Not sure what you mean by "logical unit". Yes both approaches work, but your version is about 1.14 times slower, tested on 100K vector.
zx8754
CC BY-SA 4.0
2019-01-23T13:18:18.247Z
Ah, I see. Never mind, then. By logical unit, I meant that `x`, `x[x<0.05]` and `names(obj)` are all self-contained, whereas `names(x)[boolean_vector]` is written in a way that the subsetting depends on understanding the statement as a whole.
Ram RS
CC BY-SA 4.0
2019-01-21T12:55:13.007Z
2019-01-21T12:55:13.007Z
zx8754
<p>I have two normalized gene expression values (log2 of cpm)</p>

<pre><code>&gt; head(biomarker[,1:5])
               A2        A3        A4        A6        A7
A2M    12.4071618 12.631601 12.889748 11.842511 11.574134
ABCB11  0.5151957  6.176788  5.414905  7.134915  1.247590
ABCC2   6.7244303  6.487794 10.178615  6.384132  6.089462
ABCC6   6.3977122  5.823627  7.370761  7.397091  6.071587
ABCF1   9.1609847  9.893258 10.116638 10.225520 10.838486
ABCG2   7.6841874  7.738293  6.833152  6.888041  6.030032
&gt;
</code></pre>

<p>And </p>

<pre><code>&gt; head(immune[,1:5])
              A2        A3        A4        A6        A7
A2M    10.904748 11.388404  9.910614 11.513439 12.963609
ABCB11  5.011380  6.359443  7.145992  8.451947  7.722605
ABCC2   5.040461  6.477014  3.873996  6.409777  9.133971
ABCC6   7.798441  7.601848  9.948072 11.628533 12.701460
ABCF1   8.553597  8.615425 11.145903 10.289098 11.444140
ABCG2   6.224294  5.629375  8.293416  7.979859  8.603793
&gt; 

&gt; dim(biomarker)
[1] 719  56
&gt; dim(immune)
[1] 719  56
&gt; 
</code></pre>

<p>I have matched samples in these experiments and same gene too. I could roughly say in both experiments everything has been constant. However, I want to do Student's t-Test to find genes that they had inconsistent gene expression patterns between the two data sets.</p>

<p>By this function I am getting a list but I don't know how to extract the names of genes with p-value > 0.05 or whatever cut-off</p>

<pre><code>&gt; f &lt;- function(x,y){
+     test &lt;- t.test(x,y, paired=TRUE)
+     out &lt;- data.frame(stat = test<span class="math-container">$statistic,
+                       df   = test$</span>parameter,
+                       pval = test<span class="math-container">$p.value,
+                       conl = test$</span>conf.int[1],
+                       conh = test$conf.int[2]
+     )
+     return(out)
+ }
&gt; t=sapply(seq(ncol(t(biomarker))), function(x) f(t(biomarker)[,x], t(immune)[,x]))
</code></pre>

<p>Any help please?</p>

<pre><code>  str(t)

    [list output truncated]
     - attr(*, "dim")= int [1:2] 5 719
     - attr(*, "dimnames")=List of 2
      ..<span class="math-container">$ : chr [1:5] "stat" "df" "pval" "conl" ...
  ..$</span> : NULL
    &gt;


&gt; sapply(t, "[[", "pval")
Error in FUN(X[[i]], ...) : subscript out of bounds
&gt;  


&gt; pvalueGenes &lt;- sapply(colnames(biomarker),function(i) t.test(biomarker[i, ],immune[i, ], paired = TRUE)<span class="math-container">$p.value)
Error in if (stderr &lt; 10 * .Machine$</span>double.eps * abs(mx)) stop("data are essentially constant") : 
  missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In mean.default(x) : argument is not numeric or logical: returning NA
2: In if (stderr &lt; 10 * .Machine$double.eps * abs(mx)) stop("data are essentially constant") :

 Show Traceback

 Rerun with Debug
 Error in if (stderr &lt; 10 * .Machine$double.eps * abs(mx)) stop("data are essentially constant") : 
  missing value where TRUE/FALSE needed
</code></pre>

CC BY-SA 4.0
2019-01-18T16:13:36.123Z
Did you try iterate by `colnames` instead? Have you searched the internet about how to get names of the variables you iterate over them? BTW you might be interested in the broom package, function `tidy`.
llrs
CC BY-SA 4.0
2019-01-18T20:32:33.363Z
Sorry I don't know how to do that
Zizogolu
CC BY-SA 4.0
2019-01-20T18:07:49.657Z
A simple google search would give you answers. You could use `tibble::rownames_to_column(biomarker, col_name)`. You could even do `biomarker$col_name <- colnames(biomarker)`
Ram RS
CC BY-SA 4.0
2019-01-20T23:49:09.217Z
Sorry what is col_name please?
Zizogolu
CC BY-SA 4.0
2019-01-21T10:04:44.583Z
@FereshTeh you don't need to apologise for not knowing something. "col_name" is an abbreviation for column name. Use the help pages of those functions Ram mentioned to understand what do you need and what do they do
llrs
CC BY-SA 4.0
2019-01-21T10:29:04.477Z
@FereshTeh Did you read and understood the help page of the `rownames_to_column` function?
llrs
CC BY-SA 4.0
2019-01-21T10:47:40.073Z
Actually, no; You know, likely I have done t-test to find differentially expressed genes. I have 719 genes and two sets of samples while the function returns a list with 56 * 4 dimension that means t-test has been done between samples while I need to extract genes with p-value > 0.05 among two sets of datasets, that is why I got confused and I don't know rownames_to_colnames would solve which of these
Zizogolu
CC BY-SA 4.0
2019-01-21T15:03:47.457Z
@FereshTeh Ok, that is something you should [edit] into the question.
llrs
CC BY-SA 4.0
2019-01-21T20:30:29.643Z
If you're going to downvote, add a reason why. Don't be passive-aggressive. It's bad for the community. Thanks!
Alex Reynolds
CC BY-SA 4.0
2019-01-22T08:17:16.207Z
@AlexReynolds It is not required to disclose, but I think that the downvote button already explain this: "does not show research effort, it is unclear or not useful". Asking for an explanation from downvoters is also passive-aggressive...
llrs
CC BY-SA 4.0
2019-01-22T16:13:25.817Z
@FereshTeh I used `col_name` as a name for the new column that will be generated by the function `rownames_to_column`. I should have used `'col_name'` (with quotes), sorry about that!
Ram RS
CC BY-SA 4.0
2019-01-22T20:43:41.987Z
@AlexReynolds I kinda agree with llrs. In addition to not showing research effort, OP has a habit of editing her posts extensively and changing the entire premise once she receives a response, and such behavior needs to be discouraged.
Ram RS
<rna-seq><r>
Extracting some part of a list
# Extracting some part of a list

(Asked by: username_0 on 2019-01-18)

I have two normalized gene expression values (log2 of cpm)
> head(biomarker[,1:5])
               A2        A3        A4        A6        A7
A2M    12.4071618 12.631601 12.889748 11.842511 11.574134
ABCB11  0.5151957  6.176788  5.414905  7.134915  1.247590
ABCC2   6.7244303  6.487794 10.178615  6.384132  6.089462
ABCC6   6.3977122  5.823627  7.370761  7.397091  6.071587
ABCF1   9.1609847  9.893258 10.116638 10.225520 10.838486
ABCG2   7.6841874  7.738293  6.833152  6.888041  6.030032
>

And 
> head(immune[,1:5])
              A2        A3        A4        A6        A7
A2M    10.904748 11.388404  9.910614 11.513439 12.963609
ABCB11  5.011380  6.359443  7.145992  8.451947  7.722605
ABCC2   5.040461  6.477014  3.873996  6.409777  9.133971
ABCC6   7.798441  7.601848  9.948072 11.628533 12.701460
ABCF1   8.553597  8.615425 11.145903 10.289098 11.444140
ABCG2   6.224294  5.629375  8.293416  7.979859  8.603793
> 

> dim(biomarker)
[1] 719  56
> dim(immune)
[1] 719  56
> 

I have matched samples in these experiments and same gene too. I could roughly say in both experiments everything has been constant. However, I want to do Student's t-Test to find genes that they had inconsistent gene expression patterns between the two data sets.
By this function I am getting a list but I don't know how to extract the names of genes with p-value > 0.05 or whatever cut-off
> f <- function(x,y){
+     test <- t.test(x,y, paired=TRUE)
+     out <- data.frame(stat = test$statistic,
+                       df   = test$parameter,
+                       pval = test$p.value,
+                       conl = test$conf.int[1],
+                       conh = test$conf.int[2]
+     )
+     return(out)
+ }
> t=sapply(seq(ncol(t(biomarker))), function(x) f(t(biomarker)[,x], t(immune)[,x]))

Any help please?
  str(t)

    [list output truncated]
     - attr(*, "dim")= int [1:2] 5 719
     - attr(*, "dimnames")=List of 2
      ..$ : chr [1:5] "stat" "df" "pval" "conl" ...
  ..$ : NULL
    >


> sapply(t, "[[", "pval")
Error in FUN(X[[i]], ...) : subscript out of bounds
>  


> pvalueGenes <- sapply(colnames(biomarker),function(i) t.test(biomarker[i, ],immune[i, ], paired = TRUE)$p.value)
Error in if (stderr < 10 * .Machine$double.eps * abs(mx)) stop("data are essentially constant") : 
  missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In mean.default(x) : argument is not numeric or logical: returning NA
2: In if (stderr < 10 * .Machine$double.eps * abs(mx)) stop("data are essentially constant") :

 Show Traceback

 Rerun with Debug
 Error in if (stderr < 10 * .Machine$double.eps * abs(mx)) stop("data are essentially constant") : 
  missing value where TRUE/FALSE needed

**Tags:** rna-seq, r
> **Comments:**
> **username_1**: Did you try iterate by `colnames` instead? Have you searched the internet about how to get names of the variables you iterate over them? BTW you might be interested in the broom package, function `tidy`.  
> **username_0**: Sorry I don't know how to do that  
> **username_2**: A simple google search would give you answers. You could use `tibble::rownames_to_column(biomarker, col_name)`. You could even do `biomarker$col_name <- colnames(biomarker)`  
> **username_0**: Sorry what is col_name please?  
> **username_1**: @FereshTeh you don't need to apologise for not knowing something. "col_name" is an abbreviation for column name. Use the help pages of those functions Ram mentioned to understand what do you need and what do they do  
> **username_1**: @FereshTeh Did you read and understood the help page of the `rownames_to_column` function?  
> **username_0**: Actually, no; You know, likely I have done t-test to find differentially expressed genes. I have 719 genes and two sets of samples while the function returns a list with 56 * 4 dimension that means t-test has been done between samples while I need to extract genes with p-value > 0.05 among two sets of datasets, that is why I got confused and I don't know rownames_to_colnames would solve which of these  
> **username_1**: @FereshTeh Ok, that is something you should [edit] into the question.  
> **username_3**: If you're going to downvote, add a reason why. Don't be passive-aggressive. It's bad for the community. Thanks!  
> **username_1**: @AlexReynolds It is not required to disclose, but I think that the downvote button already explain this: "does not show research effort, it is unclear or not useful". Asking for an explanation from downvoters is also passive-aggressive...  
> **username_2**: @FereshTeh I used `col_name` as a name for the new column that will be generated by the function `rownames_to_column`. I should have used `'col_name'` (with quotes), sorry about that!  
> **username_2**: @AlexReynolds I kinda agree with llrs. In addition to not showing research effort, OP has a habit of editing her posts extensively and changing the entire premise once she receives a response, and such behavior needs to be discouraged.  

---

## 2 answers

### Answer by username_3 on 2019-01-21. Score: 2 | Accepted answer

Starting from the definition of t:
...
> t <- sapply(seq(ncol(t(biomarker))), function(x) f(t(biomarker)[,x], t(immune)[,x]))

Set the column names to the gene names from one or the other of your conditions:
> colnames(t) <- rownames(immune)

Set the cutoff:
> cutoff <- 0.05

Filter for rows which meet the p-value cutoff criterium, and get their column names (gene names):
> colnames(t)[which(unlist(lapply(t["pval",], function(x) x < cutoff)))]
[1] "ABCC6" 
...

Only one gene name is reported here, using your demo datasets. Adjust your cutoff and criterium logic, as needed. For example, I filter here for gene names where the p-value is less than the cutoff; however, from your question, you may be asking for the inverse, for whatever reason, so you could change < to >=.

> **Comments:**
> **username_3**: I might suggest changing the variable name `t` to something else, as `t()` takes the transpose of the argument, if of matrix or data frame type. Using non-ambiguous variable names will make it easier for you to make changes or fix problems down the road.  

---

### Answer by username_4 on 2019-01-21. Score: 2

From your example we are comparing samples not genes, I think we need to run the test row wise, something like this:
pvalueGenes <- sapply(colnames(biomarker),
                      function(i) t.test(biomarker[i, ],
                                         immune[i, ], paired = TRUE)$p.value)

names(pvalueGenes)[ pvalueGenes < 0.05 ]

> **Comments:**
> **username_0**: Thank you, I added output of what you suggested in my post  
> **username_0**: Sorry, I have 2 matched matrices with matched samples and genes I want to find genes with p-value < 0.05 between 2 matrices but I could not do that  
> **username_4**: @FereshTeh forget your function `f`, there is no need, just run the commands in my answer. And check what you see in `pvalueGenes` object.  
> **username_0**: Thank you but your code itself giving error please have a look at error I added to my post  
> **username_4**: @FereshTeh The reason for error is statistics, read about it [more here](https://stats.stackexchange.com/q/112972/6454). Suggestion, when you get an error try to use the search, 99% of the time the error has been searched before and you will find the solution/explanation.  
> **username_2**: @zx8754, although `names(x)[ x < 0.05 ]` works, would it not be better to use `names(x[ x < 0.05 ])`, so each layer is a logical unit?  
> **username_4**: @RamRS Not sure what you mean by "logical unit". Yes both approaches work, but your version is about 1.14 times slower, tested on 100K vector.  
> **username_2**: Ah, I see. Never mind, then. By logical unit, I meant that `x`, `x[x<0.05]` and `names(obj)` are all self-contained, whereas `names(x)[boolean_vector]` is written in a way that the subsetting depends on understanding the statement as a whole.  

---

