I am using this example data.frame
and code. I would like to do multiple tests between two group, but when there are no data in one group, it is causing an error. How can I skip the comparisons without two groups and still run the code on the other ones?
library(dplyr)
df <- data.frame(group=c(rep(0,10),rep(1,10)),
apple = as.numeric(c(runif(20, -1, 18))),
pear = as.numeric(c(rep("NA",12), runif(8, 2, 7))),
banana = as.numeric(c(runif(10, 1, 3), runif(10, 2.5, 6))),
cherry = as.numeric(c(runif(9, 5, 12), rep("NA",11))))
df_new <- df %>% summarise(across(!group, ~wilcox.test(.x ~ group)$p.value), exact=NULL) %>%
bind_rows(., p.adjust(., method = 'BH')) %>%
bind_rows(df, .) %>%
mutate(group=replace(group, is.na(group), c('p.values', 'adjusted_p.values')))
# Error in `summarise()`:
# ! Problem while computing `..1 = across(!group, ~wilcox.test(.x ~ group)$p.value)`.
# Caused by error in `across()`:
# ! Problem while computing column `pear`.
# Caused by error in `wilcox.test.formula()`:
# ! grouping factor must have exactly 2 levels
# Run `rlang::last_error()` to see where the error occurred.