Hi Rui,
Thanks once again. I really appreciate it.
I tried using the code with the following dataset:




 
 
  Sample
  P1
  P2
  P3
  P4
  P5
  P6
  P7
  P8
  P9
  P10
 
 
  S1
  5292.9
  605.1
  5213.9
  1710.6
  1407.8
  1079.4
  1379.6
  9321.4
  6951
  1205.8
 
 
  S2
  104.6
  57.129
  625.69
  222.72
  247.46
  104.49
  330.29
  1863.7
  389.67
  216.29
 
 
  S3
  191.29
  19.282
  860.42
  147.83
  19.61
  189.22
  203.27
  1799
  369.9
  175.73
 
 
  S4
  41.553
  23.457
  267.09
  79.293
  143.09
  154.5
  52.567
  613.54
  408.86
  61.715
 
 
  S5
  671.33
  19.076
  1040.9
  319.04
  50.766
  57.445
  50.005
  1615.5
  1149.1
  163.99
 
 
  S6
  125.9
  22.296
  563.83
  236.36
  112.38
  81.581
  48.406
  2073.6
  388.4
  62.575
 
 
  S7
  78.485
  18.152
  248.18
  156.19
  322.4
  162.01
  38.379
  2786.8
  630.63
  71.163
 
 
  S8
  1355.6
  51.534
  422.51
  134.89
  202.34
  48.368
  69.45
  231.11
  1875.9
  153.18
 
 
  S9
  2167.6
  45.244
  430.73
  262.19
  365.71
  116.49
  65.663
  151.04
  3071.5
  210.55
 
 
  S10
  575.7
  24.699
  170.09
  128.64
  42.58
  31.034
  55.256
  294.67
  448.05
  226.19
 
 
  S11
  234.22
  22.594
  944.54
  118.91
  16.994
  102.67
  199.32
  2300
  192.38
  108.3
 
 
  S12
  193.38
  25.374
  829.88
  74.872
  108.1
  116.49
  175.49
  1248
  340.33
  65.022
 
 
  S13
  60.43
  15.907
  947.23
  49.12
  62.992
  189.11
  27.348
  134.46
  209.66
  67.638
 
 
  S14
  94.563
  23.304
  303.25
  36.745
  220.11
  128.72
  33.242
  135.6
  263.31
  107.4
 
 
  S15
  44.379
  17.794
  768
  26.144
  49.329
  103
  26.782
  290.04
  151.63
  66.928
 
 
  S16
  42.333
  14.671
  224.49
  19.642
  100.96
  82.624
  42.736
  94.781
  182.99
  68.087
 
 
  S17
  528.59
  49.81
  685.29
  24.981
  106.7
  72.219
  114.44
  610.74
  1779.9
  245.57
 

When I run the code, this is what I get :

     probeset1:probeset2 probeset1:probeset3 probeset1:probeset4
[1,]           0.2000000           0.1111111          0.07692308
[2,]           0.3333333           0.2000000          0.14285714
[3,]           0.4285714           0.2727273          0.20000000
[4,]           0.5000000           0.3333333          0.25000000
     probeset1:probeset5 probeset1:probeset6 probeset2:probeset1
[1,]          0.05882353          0.04761905            5.000000
[2,]          0.11111111          0.09090909            3.000000
[3,]          0.15789474          0.13043478            2.333333
[4,]          0.20000000          0.16666667            2.000000
     probeset2:probeset3 probeset2:probeset4 probeset2:probeset5
[1,]           0.5555556           0.3846154           0.2941176
[2,]           0.6000000           0.4285714           0.3333333
[3,]           0.6363636           0.4666667           0.3684211
[4,]           0.6666667           0.5000000           0.4000000
     probeset2:probeset6 probeset3:probeset1 probeset3:probeset2
[1,]           0.2380952            9.000000            1.800000
[2,]           0.2727273            5.000000            1.666667
[3,]           0.3043478            3.666667            1.571429
[4,]           0.3333333            3.000000            1.500000
     probeset3:probeset4 probeset3:probeset5 probeset3:probeset6
[1,]           0.6923077           0.5294118           0.4285714
[2,]           0.7142857           0.5555556           0.4545455
[3,]           0.7333333           0.5789474           0.4782609
[4,]           0.7500000           0.6000000           0.5000000
     probeset4:probeset1 probeset4:probeset2 probeset4:probeset3
[1,]                  13            2.600000            1.444444
[2,]                   7            2.333333            1.400000
[3,]                   5            2.142857            1.363636
[4,]                   4            2.000000            1.333333
     probeset4:probeset5 probeset4:probeset6 probeset5:probeset1
[1,]           0.7647059           0.6190476           17.000000
[2,]           0.7777778           0.6363636            9.000000
[3,]           0.7894737           0.6521739            6.333333
[4,]           0.8000000           0.6666667            5.000000
     probeset5:probeset2 probeset5:probeset3 probeset5:probeset4
[1,]            3.400000            1.888889            1.307692
[2,]            3.000000            1.800000            1.285714
[3,]            2.714286            1.727273            1.266667
[4,]            2.500000            1.666667            1.250000
     probeset5:probeset6 probeset6:probeset1 probeset6:probeset2
[1,]           0.8095238           21.000000            4.200000
[2,]           0.8181818           11.000000            3.666667
[3,]           0.8260870            7.666667            3.285714
[4,]           0.8333333            6.000000            3.000000
     probeset6:probeset3 probeset6:probeset4 probeset6:probeset5
[1,]            2.333333            1.615385            1.235294
[2,]            2.200000            1.571429            1.222222
[3,]            2.090909            1.533333            1.210526
[4,]            2.000000            1.500000            1.200000


As you can see P1:P2 is not 0.2000000 as indicated by the results of the code.

Am I doing something wrong?

Thanks once again.
Som.

Date: Sat, 12 May 2012 14:04:11 -0700
From: ml-node+s789695n4629513...@n4.nabble.com
To: genome1...@hotmail.com
Subject: RE: Calculating all possible ratios



        Hello,


I'm glad it helped. Now to make the result readable. (I had thought of it.)


pairwise.ratios <- function(x, prefix="probeset", char=":"){

        n <- ncol(x)

        cn <- colnames(x)

        if(length(cn) == 0){

                cn <- gsub(" ", "0", formatC(seq.int(n), width=nchar(n)))

                cn <- paste(prefix, cn, sep="")

        }

        cmb <- combn(n, 2)

        r1 <- apply(cmb, 2, function(j) x[, j[1]]/x[, j[2]])

        r2 <- apply(cmb, 2, function(j) x[, j[2]]/x[, j[1]])

        colnames(r1) <- apply(cmb, 2, function(j) paste(cn[j], collapse=char))

        colnames(r2) <- apply(cmb, 2, function(j) paste(cn[rev(j)], 
collapse=char))

        cbind(r1, r2)[, order(c(colnames(r1), colnames(r2)))]

}


m1 <- matrix(1:24, ncol=6)

pairwise.ratios(m1)


Rui Barradas


genome1976 wrote
Thanks so much Rui. I really appreciate all the help.


I implemented the code and it ran fine. I was wondering how I could include the 
probeset ids as probeset1:probeset2 in the final output so that I know which 
ratios are for which probeset pairs.



Thanks so much.

Som.

Date: Sat, 12 May 2012 09:29:54 -0700

From: [hidden email]
To: [hidden email]
Subject: Re: Calculating all possible ratios




        Hello,



If by all possible gene ratios you mean all pairwise column ratios, try 


the following.



# Make up some data


x <- matrix(1:24, ncol=6)



cmb <- combn(ncol(x), 2)


r1 <- apply(cmb, 2, function(j) x[, j[1]]/x[, j[2]])


r2 <- apply(cmb, 2, function(j) x[, j[2]]/x[, j[1]])


cbind(r1, r2)



Note that the dataset must be a numeric only matrix or data.frame.



Hope this helps,



Rui Barradas



Em 12-05-2012 11:00, [hidden email] escreveu:


> Date: Fri, 11 May 2012 12:39:23 -0700 (PDT)


> From: genome1976<[hidden email]>


> To:[hidden email]


> Subject: [R] Calculating all possible ratios


> Message-ID:<[hidden email]>


> Content-Type: text/plain


>


> I have a data matrix with  genes as columns and samples as rows. I want to


> create all possible gene ratios.Is there an elegant and fast way to do it in


> R and write it to a dataframe?


>


> Thanks for any help.


> Som.


>


> --


> View this message in 
> context:http://r.789695.n4.nabble.com/Calculating-all-possible-ratios-tp4627405.html
> Sent from the R help mailing list archive at Nabble.com.


>       [[alternative HTML version deleted]]


>


______________________________________________


[hidden email] mailing list


https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



        

        


        


        

        

                If you reply to this email, your message will be added to the 
discussion below:

                
http://r.789695.n4.nabble.com/Calculating-all-possible-ratios-tp4627405p4629105.html
        

        

                

                To unsubscribe from Calculating all possible ratios, click here.


                NAML



        
        

        

        
        
                If you reply to this email, your message will be added to the 
discussion below:
                
http://r.789695.n4.nabble.com/Calculating-all-possible-ratios-tp4627405p4629513.html
        
        
                
                To unsubscribe from Calculating all possible ratios, click here.

                NAML
                                                  

--
View this message in context: 
http://r.789695.n4.nabble.com/Calculating-all-possible-ratios-tp4627405p4629644.html
Sent from the R help mailing list archive at Nabble.com.
        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to