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