![](/img/trans.png)
[英]How to apply a function and assign multiple variables to a grouped 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.values進行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
返回的向量的長度與輸入向量的長度相同,所以您可以簡單地執行以下操作:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.