magsd magsd - 8 days ago 8
R Question

Wilcox.test between data frames in R

I want to apply the wilcox.test to each row of my two dataframes in R. For instance, to row 1 in df1 and row 1 in df2, to see if they differ significantly. I have hundreds of rows and expect hundreds of P-values to be the outcome. There are 105 columns. I am not quite sure how to write a command that does the test for each of my row pairs, since there are hundreds of them. Any help is appreciated!


Using the following data as an example:

#2 numeric data.frames (all columns are numeric)
#5 rows and 100 columns
df1 <-, nrow=5, ncol=100))
df2 <-, nrow=5, ncol=100))


#A single lapply is enough to run the wilcox test for each row
lapply(1:nrow(df1), function(i) {
  #you run the wilcox.test for each pair of rows and return the p.value
  wilcox.test(as.numeric(df1[i, ]), as.numeric(df2[i, ]))$p.value


> lapply(1:nrow(df1), function(i) {
+ wilcox.test(as.numeric(df1[i, ]), as.numeric(df2[i, ]))$p.value
+ })
[1] 0.8690001

[1] 0.1390142

[1] 0.7479788

[1] 0.5340455

[1] 0.8459806