2017-05-05 26 views
0

,我有以下的數據幀:如何在分組值函數應用在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 

我想你做的是爲P做p.value調整。值由src分組。例如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?

回答

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 
相關問題