Question

I have the following example data set

Sites<-c("A","A","B","B","B","C","C","C")
TR<-c(0.1,0.4,0.4,0.3,0.3,0.6,0.5,0.5)
df<-data.frame(Sites,TR)

I would like to subset the rows by the variable "Sites" and then bootstrap each site trap rate (TR). I have tried using the following code

require(iterators)
sites<-isplit(df,df$Sites)
nextElem(sites)
require(foreach)
mT<-foreach(Sites=sites)%do%

{
meanTraps <- vector()
for(i in 1:boots){
  meanTraps[i] <- mean(sample(sites$value$TR, nrow(sites$value), replace = TRUE))
}
}

But this just returns

list()

Ideally I'd like a data frame with meanTraps for each site. Can anyone help correct my error?

Was it helpful?

Solution

stat<-function(x,i) c(m1=mean(x$TR[i]))
library(boot)
Trap=dlply(df,.(df$Sites),function(dat) boot(dat,stat,R=10))
ldply(Trap,.fun=function(x) data.frame(mean=mean(x$t,std.err=sd(x$t)))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top