简体   繁体   中英

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. 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

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.

             [,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:

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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