简体   繁体   English

在R中使用for循环时,如何从稀有命令获得多条输出

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

I am using rarefy in the vegan package of R to rarefy species richness to a user defined total count. 我在R的纯素食包中使用稀有植物,以使稀有物种丰富度达到用户定义的总计数。 The rarefaction routine is run or looped many times and a mean values is the output. 稀疏例程运行或循环多次,并且输出平均值。 I have created a function to do this analysis on multiple columns of data (each a different site). 我创建了一个函数来对多列数据(每个不同的站点)进行此分析。 x is a data frame similar to the following x是类似于以下内容的数据帧

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

The function is as follows 功能如下

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

The function runs fine and gives the following output as long as I select FALSE within the rarefy command. 只要我在稀有命令中选择FALSE,该函数就可以正常运行并提供以下输出。

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

If I select TRUE which calculates a standard error for the mean and is a second piece of output within each loop, I get the following warning: 如果我选择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

What I want is to have that standard error value appear below the mean value like this 我想要的是使标准误差值像这样的平均值以下出现

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

How do I get this second piece of output within each for loop? 如何在每个for循环中获取第二部分输出?

I don't quite understand what you try to achieve when you loop over columns of data (which are sites in your case). 我不太了解当您遍历数据列(在您的情况下是站点)时要尝试实现的目标。 Wouldn't the following do what you try to do? 以下不是您要做的事情吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM