0

I have a data.frame of which I try to test (with WilcoxRankSum-Test) 1st and 2nd row, then 3rd and 4th row and so on. Therefore I wrote a loop. Because I have to apply this to multiple data frames, I created a function to store this loop and apply it to others later. I tried to reset the brackets {} several times or to replace pval[i] with sapply as recommended in several other threads.

In my opinion the loops works fine. But the loop cannot access pval. Sorry to bother you with a common problem. I am a newly self-taught (all from threads ;) ). But I cannot find the problem. If you see shortcuts to make life easier, I really would be glad about any comments.

Here is my code:

ZP <- function(data){  
library(exactRankTests)
pval  <- vector(length=nrow(data))
k  <- seq(1,nrow(data)-1, by=2)
for (i in seq_along(k)) {
pval[i] <-  wilcox.exact(as.numeric(data[k[i],6:10]),
                           as.numeric(data[k[i]+1,6:10]), 
                           alternative = "greater", conf.level=0.95)$p.value 
return(pval)
}
} 

Here is the result:

ZP(realdata)
 [1] 0.8492063 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
 [9] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
[17] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
[25] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000

I want it to look like this, so I can cbind it to the data later:

ZP(realdata)
 [1] "pval" "0"    "pval" "0"    "pval" "0"    "pval" "0"    "pval" "0"    "pval"
[12] "0"    "pval" "0"    "pval" "0"    "pval" "0"    "pval" "0"    "pval" "0"   
[23] "pval" "0"    "pval" "0"    "pval" "0"    "pval" "0"    "pval" "0"   

My data looks like:

      Proband Lauf Interleukin Ansatz  Zeitpunkt Data1 Data2 Data3 Data4 Data5
 1       1    1        IFNy   stim     ZP0         7     2     3     3     7
 2       1    1        IFNy    neg     ZP0         3     2     0     2     1
4

3 回答 3

0

Here's a strategy with a little less code:

library(exactRankTests)
# sample data
realdata <- data.frame(matrix(rnorm(1000),nrow=40))

# odd row indices
x <- seq(1,nrow(realdata)-1,by=2)

pvals <- vector(length=length(x)) # empty vector
for(i in seq_along(x))
    pvals[i] <- wilcox.exact(as.numeric(realdata[x[i],]),
                             as.numeric(realdata[x[i]+1,]),
                             alternative="greater",
                             conf.level=0.95)$p.value
于 2013-07-30T12:57:40.677 回答
0

How about this attempt (which might potentially work canonically with wilcox.exact())?

# data <- iris[rep(1:50,each=2)+c(0,50),] # use some flowers for demonstration
p.vals <- as.numeric(by(data[,1:4],rep(seq(50),each=2), function(rowpair) 
    wilcox.test(rowpair[,1], rowpair[,2], alt="greater")$p.val))

Btw: In case you are only interested in p.val, specifying conf.level=0.95 seems dispensable.

于 2013-07-30T13:46:38.257 回答
0

So I finally got my loop to work. Once I ran the function and applied it to a set of data, it will return my values. But when R shows me pval, which I set to '0' before, its still 0. I cannot seem to access pval outside of the function. Its probably something basic I am missing.

P <- function(x){  
  library(exactRankTests)
  pval  <- vector(length=nrow(x))
  k  <- seq(1, nrow(x), by=2)
  for (i in seq_along(k)) {
  pval[k[i]] <-  wilcox.exact(as.numeric(x[k[i],6:10]),
                              as.numeric(x[k[i]+1,6:10]), 
                              alternative = "greater", conf.level=0.95)$p.value} 
   return(pval)
}
于 2013-12-04T19:47:27.017 回答