简体   繁体   中英

Automate Mann Whitney U test for huge dataset

I am an R newby, and I apologize in advance if the question is too trivial for you people, but I'm in all over my head.

While trying to learn R, I found myself faced with the problem of creating a repeat loop to perform Mann Whitney U test group 1 vs group 2 for multiple variables, one by one. Here below, an example. ( https://i.redd.it/2c0429k4y3y21.png )

Even though I can perform the test one variable by one variable, from A to Z, in the real data I am testing I have over 10000 variables, hence I must find a way to 1) automate the test; 2) generate a file containing all the resulting p-values.

Would somebody be so kind to help me out with the task? I'm more than willing to spend as much time as I need to learn how to code in R, but I need a little nudge on this.

Thanks

As the data you supplied only had one factor level, I added three "Wildtypes" to make the code work.

gen <- structure(list(genotype=structure(c(1L, 1L, 1L, 1L, 1L, 1L),
    .Label=c("Mutant", "Wildtype"), class="factor"), X312=c(0, 0, 9.927911044,
    7.604660497, 0, 8.469434699), X1.Sep=c(9.296165425, 7.994991396, 10.3226941,
    10.59396298, 10.2554214, 7.963356173), X2.Sep=c(12.0207487, 10.92364072,
    11.22504751, 11.2077482, 11.91886469, 11.64801165), X1.Dec=c(0L, 0L, 0L, 0L, 0L,
    0L), X128up=c(8.051389852, 8.437100325, 2.9382856, 9.05631996, 0, 8.993819702),
    X140up=c(7.859521468, 7.638131579, 0, 8.567090791, 8.7672994, 10.219634)),
    row.names=c(NA, 6L), class="data.frame")


gen[1:3, 1] <- factor(rep(2, 3), label="Wildtype")

As we can perform the Mann Whitney U test on one column like so:

# for column 6
wilcox.test(gen[gen$genotype == "Mutant", 6], 
            gen[gen$genotype == "Wildtype", 6], exact=FALSE)$p.val

we can perform it on all the columns by simply looping over the column indices (excluding the first), using sapply() , like so:

sapply(2:ncol(gen), 
  function(x) {
      wilcox.test(gen[gen$genotype == "Mutant", x], 
                  gen[gen$genotype == "Wildtype", x], exact=FALSE)$p.val
  }
)
# [1] 1.00000 1.00000 1.00000     NaN 0.66252 0.08085

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM