User:Timothee Flutre/Notebook/Postdoc/2012/02/29: Difference between revisions

From OpenWetWare
Jump to navigationJump to search
(→‎Entry title: add simplification)
Line 6: Line 6:
| colspan="2"|
| colspan="2"|
<!-- ##### DO NOT edit above this line unless you know what you are doing. ##### -->
<!-- ##### DO NOT edit above this line unless you know what you are doing. ##### -->
==Entry title==
==Use R to extract the best SNP among all in cis of any given gene==


* In R, extract one row for each level of a column based on the value of a second column:
This can be reformulated more generically as "extract one row for each level of a column based on the value of a second column":


Let's create a dummy dataset:
Let's create a dummy dataset:

Revision as of 04:56, 11 April 2012

Project name <html><img src="/images/9/94/Report.png" border="0" /></html> Main project page
<html><img src="/images/c/c3/Resultset_previous.png" border="0" /></html>Previous entry<html>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</html>Next entry<html><img src="/images/5/5c/Resultset_next.png" border="0" /></html>

Use R to extract the best SNP among all in cis of any given gene

This can be reformulated more generically as "extract one row for each level of a column based on the value of a second column":

Let's create a dummy dataset:

x <- data.frame("g"=c("g2","g1","g2","g1"), "s"=c("s1","s2","s3","s4"), p=c(10^-4,10^-3,10^-2,10^-5))

Here is how it looks like:

   g  s     p
1 g2 s1 1e-04
2 g1 s2 1e-03
3 g2 s3 1e-02
4 g1 s4 1e-05

For instance, the "g" column indicates gene names, the "s" column indicates SNP names, and the "p" column indicates P-values of association between genotypes at the SNP and variation in gene expression levels. In such a case, I want to extract the best SNP for each gene, ie. those with the lowest P-value.

First, I sort the "g" column according to the P-values and I keep the row indices after sorting:

v <- x$g[i <- order(x$p)]
v
[1] g1 g2 g1 g2
i
[1] 4 1 2 3

Second, I find the first occurrence of each level in column "g" (in this example, the first occurrence corresponds to the lowest P-value for this gene):

min.occ <- !duplicated(v)
min.occ
[1]  TRUE  TRUE FALSE FALSE

Third, I retrieve the row indices corresponding to these first occurrences per level:

row.idx <- setNames(seq_len(nrow(x))[i][min.occ], v[min.occ])
row.idx
g1 g2
 4  1

Finally, I extract the data I'm interested in:

x[row.idx,]
   g  s     p
4 g1 s4 1e-05
1 g2 s1 1e-04

This could also be done more simply by:

x[i[min.occ],] 
   g  s     p
4 g1 s4 1e-05
1 g2 s1 1e-04

And according to this answer on SO, the whole procedure seems pretty fast!