问题
I want to apply pairwise.wilcox.test
function using dplyr
package. I am using the following code
library(tidyverse)
tbl_df(df)%>%
pivot_longer(cols = -Class, names_to = "Wavelengths", values_to = "value") %>%
group_by(Wavelengths) %>%
summarize(pairwise.wilcox.test(value, as.factor(Class), p.adjust.method="bonf")$p.value)
It is giving me the following error
Error: Column
pairwise.wilcox.test(try$value, as.factor(try$Class), p.adjust.method = "bonf")$p.value
must be length 1 (a summary value), not 16
How to eliminate the error?
Update
As suggested by @duckmayr, I have updated my dplyr
package (Newest version 1.0.0) and the error is not coming and it is giving me the following output
#> `summarise()` regrouping output by 'Wavelengths' (override with `.groups` argument)
#> # A tibble: 16 x 2
#> # Groups: Wavelengths [4]
#> Wavelengths pval[,1] [,2] [,3] [,4]
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 WV_350 1 NA NA NA
#> 2 WV_350 0.0794 0.0794 NA NA
#> 3 WV_350 0.0794 0.0794 1 NA
#> 4 WV_350 0.0794 0.0794 0.749 0.556
#> 5 WV_351 1 NA NA NA
#> 6 WV_351 0.0794 0.0794 NA NA
#> 7 WV_351 0.0794 0.0794 1 NA
#> 8 WV_351 0.0794 0.0794 0.556 0.556
#> 9 WV_352 1 NA NA NA
#> 10 WV_352 0.0794 0.0794 NA NA
#> 11 WV_352 0.0794 0.0794 1 NA
#> 12 WV_352 0.0794 0.0794 0.556 0.749
#> 13 WV_353 1 NA NA NA
#> 14 WV_353 0.0794 0.0794 NA NA
#> 15 WV_353 0.0794 0.0794 1 NA
#> 16 WV_353 0.0794 0.0794 0.556 0.317
As you can see from the above that the output is not coming in proper shape i.e. one more column of class should come. As of now, it is showing only class 1. I want to melt
the output farther like Wavelengths Group1 Group2 pvalue
, so that the output comes in a better format.
Data
df = structure(list(Class = c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3,
3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5), WV_350 = c(0.0196, 0.0206,
0.023, 0.0264, 0.029, 0.0201, 0.0181, 0.0216, 0.0225, 0.019,
0.0165, 0.0121, 0.0129, 0.0123, 0.0149, 0.0137, 0.0116, 0.0151,
0.0138, 0.0167, 0.0149, 0.0112, 0.0107, 0.01, 0.0099), WV_351 = c(0.0197,
0.0206, 0.0229, 0.0265, 0.029, 0.0199, 0.0183, 0.0216, 0.0225,
0.0187, 0.0165, 0.0118, 0.0127, 0.0122, 0.0148, 0.0138, 0.0114,
0.0145, 0.0132, 0.0164, 0.0144, 0.0108, 0.01, 0.0093, 0.0095),
WV_352 = c(0.0199, 0.0207, 0.0233, 0.027, 0.0299, 0.0203,
0.0186, 0.0219, 0.0232, 0.019, 0.0169, 0.0124, 0.0133, 0.0126,
0.0152, 0.0145, 0.0118, 0.0148, 0.0132, 0.0168, 0.0148, 0.0111,
0.0102, 0.0096, 0.0098), WV_353 = c(0.0204, 0.0213, 0.0238,
0.0277, 0.0307, 0.0208, 0.0194, 0.0229, 0.0241, 0.0199, 0.0173,
0.013, 0.0142, 0.0134, 0.0161, 0.0152, 0.0126, 0.0153, 0.0137,
0.0175, 0.0151, 0.0116, 0.0105, 0.01, 0.0098)), row.names = c(NA,
25L), class = "data.frame")
回答1:
A bit verbose and Im sure it could be made more efficient:
library(tidyverse)
library(broom)
res <-
tbl_df(df)%>%
pivot_longer(cols = -Class, names_to = "Wavelengths", values_to = "value") %>%
group_by(Wavelengths) %>%
summarise(pw_wt = list(pairwise.wilcox.test(value,as.factor(Class),
p.adjust.method = "bonf")$p.value)) %>%
ungroup() %>%
mutate(pw_wt_t = map(pw_wt, broom::tidy)) %>%
unnest(pw_wt_t)
来源:https://stackoverflow.com/questions/62129863/how-to-melt-pairwise-wilcox-test-output-using-dplyr