2016-02-15 57 views
0

這裏我已經定義了功能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 
    } 
} 
+2

你需要它知道什麼返回 – jalapic

+0

添加'return'該函數實際上是在最後一行 - 如果for循環被替換爲'sapply',那麼_would會是一個返回值。從技術上講,這更像是一個「爲什麼這個循環返回null」的問題,而不是一個鏈接問題的重複 – rawr

回答

0

因爲你需要返回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 
} 
相關問題