Error in match.arg(p.adjust.method) : 'arg'

2019-09-05 00:46发布

问题:

Here my data

mydat=structure(list(id = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), group = c(1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L), var = c(23L, 24L, 24L, 23L, 23L, 
24L, 24L, 23L, 23L, 24L, 24L, 23L, 23L, 24L, 24L, 23L, 23L, 24L, 
24L, 23L, 23L, 24L, 24L, 23L)), .Names = c("id", "group", "var"
), class = "data.frame", row.names = c(NA, -24L))

I want to join two tables. id is identificator.

library(tidyverse)
mdyat %>% 
  with(.,pairwise.wilcox.test(var,id, group, exact =F)) %>% 
  broom::tidy() %>% 
 complete(id,group) %>% 
  left_join(mydat %>% 
              group_by(id,group)) %>% 
              summarise_all(c("mean", "sd", "median")) 
            by=c("id,group")

and get the error

Error in match.arg(p.adjust.method) : 
  'arg' must be NULL or a character vector

How to do that this script performed for each indentificator separately I.E. Desired output

id      mean    sd      median  p.value
1   1   23,5    0.5773503   23,5    NA
1   2   23,5    0.5773503   23,5    1
1   3   23,5    0.5773503   23,5    1
2   1   23,5    0.5773503   23,5    NA
2   2   23,5    0.5773503   23,5    1
2   3   23,5    0.5773503   23,5    1

回答1:

The first part can be fixed using group_by and do as follows.

mydat %>% 
  group_by(id) %>%
  do({
    with(., pairwise.wilcox.test(var, group, exact =F)) %>% broom::tidy()
  }) 

 ## # A tibble: 6 x 4
 ## # Groups:   id [2]
 ##      id group1 group2 p.value
 ##   <int> <fctr>  <chr>   <dbl>
 ## 1     1      2      1       1
 ## 2     1      3      1       1
 ## 3     1      3      2       1
 ## 4     2      2      1       1
 ## 5     2      3      1       1
 ## 6     2      3      2       1

In order to combine this with the summary statistics, you need to decide which group you want to join with (group1 or group2). In the following I joined with group1, so the mean, sd and median refer to group1 and the p.value refers to the difference between group1 and group2.

mydat %>% 
  group_by(id) %>%
  do({
    with(., pairwise.wilcox.test(var, group, exact =F)) %>% broom::tidy()
  }) %>% 
  mutate(group1 = as.numeric(as.character(group1)), 
         group2 = as.numeric(as.character(group2))) %>%
  complete(group1 = mydat$group) %>%
  left_join(mydat %>% group_by(id,group) %>% summarise_all(c("mean", "sd", "median")), 
            by=c('id', 'group1'='group'))


回答2:

Your function arguments are wrong:

pairwise.wilcox.test(var,id, group, exact =F)

?pairwise.wilcox.test states the correct syntax as:

pairwise.wilcox.test(x, g, p.adjust.method = p.adjust.methods,
                      paired = FALSE, ...)

which means the third function argument should be p.adjust.method, not group.



标签: r dplyr tidyr