我有以下数据框:
df <- structure(list(src = structure(c(1L, 2L, 1L, 2L, 1L, 2L), .Label = c("s1",
"s2"), class = "factor"), ref = structure(c(1L, 1L, 2L, 2L, 3L,
3L), .Label = c("K1", "K2", "K3"), class = "factor"), p.value = c(7.70538659065046e-07,
0.0109433917493518, 3.68576080132045e-07, 0.0194953188963631,
6.3909178521645e-06, 0.00181897125900132)), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"), .Names = c("src",
"ref", "p.value"))
df
#> src ref p.value
#> 1 s1 K1 7.705387e-07
#> 2 s2 K1 1.094339e-02
#> 3 s1 K2 3.685761e-07
#> 4 s2 K2 1.949532e-02
#> 5 s1 K3 6.390918e-06
#> 6 s2 K3 1.818971e-03
我想要做的是对按src
分组的p.value进行p.value调整。例如s1我们可以进行这种调整:
> p.adjust(c( 7.705387e-07, 3.685761e-07, 6.390918e-06 ), method = "fdr")
[1] 1.155808e-06 1.105728e-06 6.390918e-06
在一天结束时希望有这样的形式:
src ref p.value FDR
1 s1 K1 7.705387e-07 1.155808e-06
2 s2 K1 1.094339e-02 0.016415088
3 s1 K2 3.685761e-07 1.105728e-06
4 s2 K2 1.949532e-02 0.019495319
5 s1 K3 6.390918e-06 6.390918e-06
6 s2 K3 1.818971e-03 0.005456913
我怎么能用tidyverse做到这一点?
答案 0 :(得分:2)
因为p.adjust
返回与输入向量长度相同的向量,所以您可以这样做:
df %>% group_by(src) %>% mutate(FDR = p.adjust(p.value, method = "fdr"))
#Source: local data frame [6 x 4]
#Groups: src [2]
# src ref p.value FDR
# <fctr> <fctr> <dbl> <dbl>
#1 s1 K1 7.705387e-07 1.155808e-06
#2 s2 K1 1.094339e-02 1.641509e-02
#3 s1 K2 3.685761e-07 1.105728e-06
#4 s2 K2 1.949532e-02 1.949532e-02
#5 s1 K3 6.390918e-06 6.390918e-06
#6 s2 K3 1.818971e-03 5.456914e-03