这是我的数据
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))
我想加入两个表。 id是标识符。
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")
并得到错误
Error in match.arg(p.adjust.method) :
'arg' must be NULL or a character vector
该脚本分别为每个标识符执行的操作方法 即所需的输出
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
答案 0 :(得分:2)
您的函数参数错误:
pairwise.wilcox.test(var,id, group, exact =F)
?pairwise.wilcox.test
指出正确的语法为:
pairwise.wilcox.test(x, g, p.adjust.method = p.adjust.methods,
paired = FALSE, ...)
这意味着第三个函数参数应该是p.adjust.method
,而不是group
。
答案 1 :(得分:2)
可以使用group_by
和do
如下固定第一部分。
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
为了将其与摘要统计信息结合起来,您需要确定要加入哪个组(group1
或group2
)。在下文中,我加入了group1
,因此mean
,sd
和median
指的是group1
,而p.value
指的是group1
和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'))