I am new to R, I have a quick question, how to apply Wilcoxon test to every row of a matrix under R? for example, as the simplest matrix. I am trying to apply the Wilcoxon test to every row by for loop, two groups of columns, columns 1 to 5 as a group, and 6 to 10 as another group. And save the P-values as a column to a file. I wrote two for loops but failed. I put mine for loops and error messages in the last. Thank you so much.
mymatrix
[,1][,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 11 21 31 41 51 61 71 81 91
[2,] 2 12 22 32 42 52 62 72 82 92
[3,] 3 13 23 33 43 53 63 73 83 93
[4,] 4 14 24 34 44 54 64 74 84 94
[5,] 5 15 25 35 45 55 65 75 85 95
[6,] 6 16 26 36 46 56 66 76 86 96
[7,] 7 17 27 37 47 57 67 77 87 97
[8,] 8 18 28 38 48 58 68 78 88 98
[9,] 9 19 29 39 49 59 69 79 89 99
[10,] 10 20 30 40 50 60 70 80 90 100
for (i in nrow(mymatrix)){
vector1 <- c(mymatrix[i,1:5])
vector2 <- c(mymatrix[i,6:10])
wilcox.test(vector1,vector2, paired = TRUE, alternative = "two.sided")
}
#Warning message:
In wilcox.test.default(vector1, vector2, paired = TRUE, alternative = "two.sided") :
cannot compute exact p-value with ties
# I also tried this, it doesn't work either.
for (i in nrow(mymatrix)){
wilcox.test(as.numeric(mymatrix[i,1:5],as.numeric(mymatrix[i,6:10]), paired = TRUE)
}
You looking for something like this:
# initialize a list to store the p_values
p_values <- vector("list", nrow(mymatrix))
for(i in seq_along(1: nrow(mymatrix))){
p_values[i] = wilcox.test(mymatrix[i,1:5],mymatrix[i,6:10], paired = TRUE, alternative = "two.sided", exact = FALSE)$p.value
}
# make it a data.frame
p_values = data.frame(p_values = sapply(p_values, c))
#Output
p_values
# p_values
# 1 0.03688843
# 2 0.03688843
# 3 0.03688843
# 4 0.03688843
# 5 0.03688843
# 6 0.03688843
# 7 0.03688843
# 8 0.03688843
# 9 0.03688843
# 10 0.03688843
Hope that helps.