rdataframematrixhypothesis-testpairwise.wilcox.test# running paired wilcoxon test on rows of two dataframes

I have a two large dataframes (around 19000 rows and 71 columns) as follows df1

sample1 | sample2 | sample3 | |
---|---|---|---|

gene1 | 5 | 10 | 15 |

gene2 | 2 | 8 | 10 |

gene3 | 3 | 9 | 10 |

df2

sample1 | sample2 | sample3 | |
---|---|---|---|

gene1 | 40 | 50 | 65 |

gene2 | 12 | 18 | 0 |

gene3 | 31 | 19 | 10 |

I am trying to perform wilcoxon rank sum test on the rows with the same index but the code is taking forever on google colab!! My code so far

```
wilc_results= c()
for( x in 1:nrow(df1)){
for (y in 1:nrow(df2)){
result= wilcox.test(as.numeric(df2[y,]), as.numeric(f1d[x,]),
alternative= 'two.sided', paired= T )
wilc_results[length(wilc_results) + 1] <- result$p.value
}
}
```

is there a much faster way to get the desired output?

Solution

There is no need to loop twice, since both your data frames have the same number of columns. It runs in about 10 seconds on a similarly sized dataset on my computer.

```
wilc_results <- list()
for(i in 1:nrow(df1)) {
result <- wilcox.test(as.numeric(df1[i,]), as.numeric(df2[i,]),
alternative='two.sided', paired=T)
wilc_results[[i]] <- result$p.value
}
```

- how to automatically compute whether all groups have the same number of rows, and if not how to search which group lacks which row
- Loop to pull every ID into dataframe from API in R
- Transforming a dataframe by creating a new column
- Using tryCatch to avoid erros in a double loop
- Plotly Radar Chart from ”circle” to polygon in R
- Replace 0 with NA in certain columns in R
- R: Yearmon producing NA values
- Creating a delta variable over time for different values of an ID
- tidyr::unnest() tibble, integer and vector
- Join lines in R plot based in order in data frame
- How to correctly install rpy2 in python?
- bslib theme not coloring page_navbar as expected
- How can I generate a random number with restrictions?
- subset of data.frame columns to maximize "complete" observations
- can't execute rsDriver (connection refused)
- Select the data.frame with maximum dimensions from a list of data.frames
- nTrials must be be greater.... issue on conjoint design
- error in `na_if()`: ! Can't convert `y` <character> to match type of `x` <double>
- Getting errors installing glmnet package using R studio
- R plotly show only labels in the stacked bar chart where percentage value for the stack is above 5
- Is there a way to add superscript and subscript to the same element using ggtext,<sub>&<sup>?
- ggplot labels with "K" for thousands or "M" for millions (maintaining 'comma' y-axis labeling)
- Default value in filter_select crosstalk doesn't work with JS
- Automated procedure to draw ray in ggplot
- group_by and pivot_wider combination
- lm(): What is qraux returned by QR decomposition in LINPACK / LAPACK
- How to apply different functions for different groups and mutate?
- R studio time column is between
- Optimizing nested ifelse in shiny
- Expanding dataset rows based on intervals in R