簡體   English   中英

在R中使用for循環時,如何從稀有命令獲得多條輸出

[英]How do I get multiple pieces of output from rarefy command when using for loops in R

我在R的純素食包中使用稀有植物,以使稀有物種豐富度達到用戶定義的總計數。 稀疏例程運行或循環多次,並且輸出平均值。 我創建了一個函數來對多列數據(每個不同的站點)進行此分析。 x是類似於以下內容的數據幀

row.names    site1    site2    site3
ameletus     0        23       10
baetis       34       21       19
.....        .        .        .
zapada       34       41       22

功能如下

bulkrarefy <- function(x,count){
sitenames <- colnames(x)rarefyresult = NULL
for (i in 1 : ncol(x)){
rarefyresult[i] <- rarefy(x[ ,i], count, FALSE)
}
finalresult <- rbind(sitenames,rarefyresult)
}
finalresult

只要我在稀有命令中選擇FALSE,該函數就可以正常運行並提供以下輸出。

             [,1]               [,2]               [,3]              
sitenames    "Site1"            "Site2"            "Site3"           
rarefyresult "46.5707576102635" "32.8694544217779" "58.4414780302239"

如果我選擇TRUE來計算平均值的標准誤,並且是每個循環中的第二條輸出,則會收到以下警告:

1: In rarefyresult[i] <- rarefy(x[, i], count, TRUE) :
  number of items to replace is not a multiple of replacement length
2: In rarefyresult[i] <- rarefy(x[, i], count, TRUE) :
  number of items to replace is not a multiple of replacement length
3: In rarefyresult[i] <- rarefy(x[, i], count, TRUE) :
  number of items to replace is not a multiple of replacement length

我想要的是使標准誤差值像這樣的平均值以下出現

             [,1]               [,2]               [,3]              
sitenames    "Site1"            "Site2"            "Site3"           
rarefyresult "46.5707576102635" "32.8694544217779" "58.4414780302239"
SE           "2.3"              "1.5"              "4.1"

如何在每個for循環中獲取第二部分輸出?

我不太了解當您遍歷數據列(在您的情況下是站點)時要嘗試實現的目標。 以下不是您要做的事情嗎?

count <- 20
rarefy(x, count, se = TRUE, MARGIN = 2)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM