【问题标题】:Why the function when loaded doesn't give any output? [duplicate]为什么加载时的函数没有任何输出? [复制]
【发布时间】:2016-02-15 02:10:36
【问题描述】:

在这里我定义了函数CpG.T.Test,并在其中运行t-test。但最后,tp 都不存在。

CpG.T.Test  <- function(betam, pheno){
  R = 50000
  t = numeric(R)
  p = numeric(R)

  for (i in 1:R){
    beta.v <- betam[i, ]
    pheno.v <- pheno

    test <- t.test (beta.v ~ pheno.v)
    t[i] = test$statistic
    p[i] = test$p.value
  }
}

【问题讨论】:

  • 你需要添加一个return它知道返回什么
  • 函数实际上在最后一行——如果for循环被替换为sapply会有返回值。从技术上讲,这更像是一个“为什么这个 for 循环返回 null”的问题,而不是一个链接问题的副本

标签: r


【解决方案1】:

由于需要返回2个变量,可以将它们组合成一个数据框,返回如下:

CpG.T.Test  <- function(betam, pheno){
  R = 50000
  t = numeric(R)
  p = numeric(R)

  for (i in 1:R){
    beta.v <- betam[i, ]
    pheno.v <- pheno

    test <- t.test (beta.v ~ pheno.v)
    t[i] = test$statistic
    p[i] = test$p.value
  }

  cbind(t,p) # the last variable used in the scope is usually returned.
  ## OR
  ## return cbind(t,p) # or you can explicitly return the variable
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 2022-01-19
    • 2021-05-17
    • 2016-07-19
    相关资源
    最近更新 更多