Awesome! Thanks so much for your help. I am able to get the format I was
looking for.


On Mon, Jun 24, 2013 at 2:50 PM, Adams, Jean <jvad...@usgs.gov> wrote:

> When sharing data, use dput() to output the data in a way that R-Help
> readers can easily use.
>
> Give this code a try and see if it helps.
>
> Jean
>
>
>
> # read in the example data
> mat1 <- structure(list("GENE SYMBOL" = c("ADCK2", "ADCK3", "ADCK4",
> "ADCK5",
>  "ADRBK1", "ADRBK2", "AKT1", "AKT2", "AKT3", "ALK"), Sample.A1.A0SK.01 =
> c(0L,
> 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L), Sample.A1.A0SO.01 = c(0L,
>  0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Sample.A1.A0SP.01 = c(0L,
> 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Sample.A2.A04P.01 = c(0L,
>  0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Sample.A2.A04Q.01 = c(0L,
> 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Sample.A2.A04U.01 = c(0L,
>  1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L), Sample.A2.A0CL.01 = c(0L,
> 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Sample.A2.A0CM.01 = c(0L,
>  0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Sample.A2.A0D0.01 = c(0L,
> 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L), Sample.A2.A0D2.01 = c(1L,
>  0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Sample.A2.A0ST.01 = c(0L,
> 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Sample.A2.A0SX.01 = c(0L,
>  1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L), Sample.A2.A0T0.01 = c(0L,
> 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("GENE SYMBOL",
>  "Sample-A1-A0SK-01", "Sample-A1-A0SO-01", "Sample-A1-A0SP-01",
> "Sample-A2-A04P-01", "Sample-A2-A04Q-01", "Sample-A2-A04U-01",
>  "Sample-A2-A0CL-01", "Sample-A2-A0CM-01", "Sample-A2-A0D0-01",
> "Sample-A2-A0D2-01", "Sample-A2-A0ST-01", "Sample-A2-A0SX-01",
>  "Sample-A2-A0T0-01"), class = "data.frame", row.names = c(NA, -10L))
> mat2 <- mat1
>
> fish <- function(x, y) {
>  n00 = sum((1-x)*(1-y))
> n01 = sum((1-x)*y)
> n10 = sum(x*(1-y))
>  n11 = sum(x*y)
> a = matrix(c(n00, n01, n10, n11), nrow=2)
> pval = fisher.test(a)$p.value
>  return(c(n00, n01, n10, n11, pval))
> }
>
> results <- data.frame(matrix(NA, nrow=dim(mat1)[1]*dim(mat2)[1], ncol=7,
> dimnames=list(NULL, c("gene1", "gene2", "n00", "n01", "n10", "n11",
> "pval"))))
> count <- 0
> for(i in 1:dim(mat1)[1]) {
> for(j in 1:dim(mat2)[1]) {
> count <- count + 1
> results[count, "gene1"] <- mat1$"GENE SYMBOL"[i]
>  results[count, "gene2"] <- mat2$"GENE SYMBOL"[j]
> results[count, 3:7] <- fish(mat1[i, -1], mat2[j, -1])
>  }}
>
>
>
> On Mon, Jun 24, 2013 at 10:35 AM, Angel Russo <angerusso1...@gmail.com>wrote:
>
>> Sample data is as follows (for simplicity assume mat1 and mat2 are the
>> same matrices). Also attached as an excel file.
>>
>> I want to get the pairwise interaction fischer test results. Not just the
>> pvalues but also want to wrote the n00, n01, n10 and n11 in the file as:
>>
>>
>> ADCK2_mat1 ADCK3_mat2 n00 n01 n10 n11 pvalue
>>
>>
>>
>> ADCK2_mat1 ADCK4_mat2 n00 n01 n10 n11 pvalue
>>
>>  GENE SYMBOL Sample-A1-A0SK-01 Sample-A1-A0SO-01 Sample-A1-A0SP-01
>> Sample-A2-A04P-01 Sample-A2-A04Q-01 Sample-A2-A04U-01 Sample-A2-A0CL-01
>> Sample-A2-A0CM-01 Sample-A2-A0D0-01 Sample-A2-A0D2-01 Sample-A2-A0ST-01
>> Sample-A2-A0SX-01 Sample-A2-A0T0-01  ADCK2 0 0 0 0 0 0 0 0 0 1 0 0 0
>> ADCK3 0 0 0 0 0 1 0 0 1 0 0 1 0  ADCK4 0 0 0 0 0 0 0 0 0 0 0 1 0  ADCK5 1
>> 0 0 0 0 0 0 0 0 0 0 0 1  ADRBK1 0 0 0 0 0 0 0 0 0 0 0 0 0  ADRBK2 0 0 0 0
>> 0 1 0 0 0 0 0 0 0  AKT1 0 0 0 0 0 0 0 0 0 0 0 0 0  AKT2 0 0 0 0 0 0 0 0 0
>> 0 0 1 0  AKT3 0 0 0 0 0 0 0 0 1 0 0 1 0  ALK 0 0 0 0 0 0 0 0 0 0 0 0 0
>>
>>
>> On Mon, Jun 24, 2013 at 11:03 AM, Adams, Jean <jvad...@usgs.gov> wrote:
>>
>>> Could you provide an example of mat1 and mat2 as well as an example of
>>> the output you would like from them.
>>>
>>> Your code is very difficult to follow as written.  It will be easier for
>>> readers of the list to interpret if you use carriage returns rather than
>>> semi colons, for example ...
>>>
>>> fish <- function(x, y) {
>>> n00 = sum((1-x)*(1-y))
>>> n01 = sum((1-x)*y)
>>>  n10 = sum(x*(1-y))
>>>  n11 = sum(x*y)
>>> a = matrix(c(n00, n01, n10, n11), nrow=2)
>>> pval = fisher.test(a)$p.value
>>>  return(pval)
>>> }
>>>
>>> Jean
>>>
>>>
>>> On Mon, Jun 24, 2013 at 6:09 AM, Angel Russo <angerusso1...@gmail.com>wrote:
>>>
>>>> **
>>>>
>>>> I am using the following way to get p-values from fiser exact test.
>>>> However, I do need to print for each pair the values "n00, n01, n10,
>>>> n11".
>>>> How can I print that as a table and not a matrix as below along with the
>>>> p-value? Any help will be greatly appreciated
>>>>
>>>>  fish <- function(y, x) {n00 = sum((1-x)*(1-y)); n01 = sum((1-x)*y);
>>>> n10 = sum(x*(1-y)); n11 = sum(x*y); a = matrix(c(n00, n01, n10, n11),
>>>> nrow = 2); pval = fisher.test(a)$p.value; return(pval);}
>>>>
>>>>  chiArray <- function(x) {  apply(mat1, 1, fish, x); }
>>>>  sapply(1:nrow(mat2), function(j){chiArray(mat2[j, ]);});
>>>>  chisq.cna.mut.test <- sapply(1:nrow(mat2),
>>>> function(j){chiArray(mat2[j, ]);});
>>>>
>>>> I want output to be:
>>>>
>>>> name1_mat1 name1_mat2 n00 n01 n10 n11 pvalue
>>>>
>>>>
>>>>
>>>> name1_mat1 name2_mat2 n00 n01 n10 n11 pvalue
>>>>
>>>>         [[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.
>>>>
>>>
>>>
>>
>

        [[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