2
我創建了一個函數來調用數據集中每一行的函數。我想將輸出作爲矢量。正如你在下面看到的,該函數將結果輸出到屏幕上,但我無法弄清楚如何將輸出重定向到一個我可以在函數外部使用的向量。向一個向量輸出函數結果
n_markers <- nrow(data)
p_values <-rep(0, n_markers)
test_markers <- function()
{
for (i in 1:n_markers)
{
hets <- data[i, 2]
hom_1 <- data[i, 3]
hom_2 <- data[i, 4]
p_values[i] <- SNPHWE(hets, hom_1, hom_2)
}
return(p_values)
}
test_markers()
1.你應該養成傳遞函數參數而不是依賴全局變量的習慣(參考http://stackoverflow.com/questions/5526322/examples-of-the-perils-of-globals-in-r-and-stata)。 2.已經有一個功能可以做到這一點。看看'apply(my.data,1,...)' –
'shinyNewVector < - test_markers()'。請注意gsk3的建議。 – Chase
請閱讀R的基本介紹。你需要將你的思想轉換爲向量而不是循環。 – Hansi