如何对 tibble 中的分组值应用函数

How to apply a function on grouped values in a tibble

我有以下数据框:


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 做到这一点?

因为p.adjust returns一个与输入向量长度相同的向量,你可以简单地做:

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