首页 文章

如何在mutate(dplyr)中使用自定义函数?

提问于
浏览
4

我正在使用dplyr重写我的所有代码,并需要mutate / mutate_at函数的帮助 . 我只需要将自定义函数应用于表格中的两列 . 理想情况下,我会通过它们的索引引用这些列,但现在我甚至无法通过名称引用它 .

功能是:

binom.test.p <- function(x) {
  if (is.na(x[1])|is.na(x[2])|(x[1]+x[2])<10) {
    return(NA)
  } 
  else {
    return(binom.test(x, alternative="two.sided")$p.value)
  }
}

我的数据:

table <- data.frame(geneId=c("a", "b", "c", "d"), ref_SG1_E2_1_R1_Sum = c(10,20,10,15), alt_SG1_E2_1_R1_Sum = c(10,20,10,15))

所以我这样做:

table %>%
  mutate(Ratio=binom.test.p(c(ref_SG1_E2_1_R1_Sum, alt_SG1_E2_1_R1_Sum)))
Error: incorrect length of 'x'

如果我做:

table %>% 
mutate(Ratio=binom.test.p(ref_SG1_E2_1_R1_Sum, alt_SG1_E2_1_R1_Sum))
Error: unused argument (c(10, 20, 10, 15))

第二个错误可能是因为我的函数需要一个向量而是获得两个参数 .

但即使忘了我的功能 . 这有效:

table %>%
  mutate(sum = ref_SG1_E2_1_R1_Sum + alt_SG1_E2_1_R1_Sum)

这不是:

table %>%
      mutate(.cols=c(2:3), .funs=funs(sum=sum(.)))
Error: wrong result size (2), expected 4 or 1

所以这可能是我对dplyr如何工作的误解 .

1 回答

  • 5

    你的问题似乎是 binom.test 而不是 dplyrbinom.test 没有矢量化,所以你不能指望它适用于矢量;您可以在 mutate 的两列上使用 mapply

    table %>% 
        mutate(Ratio = mapply(function(x, y) binom.test.p(c(x,y)), 
                              ref_SG1_E2_1_R1_Sum, 
                              alt_SG1_E2_1_R1_Sum))
    
    #  geneId ref_SG1_E2_1_R1_Sum alt_SG1_E2_1_R1_Sum Ratio
    #1      a                  10                  10     1
    #2      b                  20                  20     1
    #3      c                  10                  10     1
    #4      d                  15                  15     1
    

    至于最后一个,你需要 mutate_at 而不是 mutate

    table %>%
          mutate_at(.vars=c(2:3), .funs=funs(sum=sum(.)))
    

相关问题