0

我有以下示例数据集

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)

我想通过变量“站点”对行进行子集化,然后引导每个站点陷阱率(TR)。我尝试使用以下代码

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))
}
}

但这只是返回

list()

理想情况下,我希望每个站点都有一个带有 meanTraps 的数据框。任何人都可以帮助纠正我的错误吗?

4

1 回答 1

0
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)))
于 2014-02-25T20:42:26.620 回答