nl <- 1000 # List length
lists <- vector("list", length=nl)
for(i in 1:nl) lists[[i]] <- list(x = rnorm(100), y = rnorm(100))
str(lists)
object.size(lists) # ~ 2 MB
all <- rbind_listdf(lists)
str(all)
object.size(all) # ~ 7 MB
# Note each of following can take a few seconds to run
# Compare timings:
t0 <- Sys.time()
all <- rbind_listdf(lists)
(Sys.time() - t0)
t0 <- Sys.time()
all <- rbind_listdf(lists, seq.by=50)
(Sys.time() - t0)
t0 <- Sys.time()
all <- rbind_listdf(lists, seq.by=500)
(Sys.time() - t0)
# Compare to non-function version
all2 <- data.frame()
t0 <- Sys.time()
for(i in 1:nl) all2 <- rbind(all2, lists[[i]])
(Sys.time() - t0)
# Build blank ecospace framework to use in simulations
ecospace <- create_ecospace(nchar=15, char.state=rep(3, 15), char.type=rep("numeric", 15))
# Build 25 samples for neutral model:
nreps <- 1:30
n.samples <- lapply(X=nreps, FUN=neutral, Sseed=3, Smax=10, ecospace)
# Calculate functional diversity metrics for simulated samples
n.metrics <- lapply(X=nreps, FUN=calc_metrics, samples=n.samples, Model="neutral", Param="NA")
alarm()
str(n.metrics)
# rbind lists together into a single dataframe
all <- rbind_listdf(n.metrics)
# Calculate mean dynamics (not reliable given only 10 replicates)
means <- n.metrics[[1]]
for(n in 1:10) means[n,4:11] <- apply(all[which(all$S==means$S[n]),4:11], 2, mean, na.rm=TRUE)
means
# Plot statistics as function of species richness, overlaying mean dynamics
op <- par()
par(mfrow=c(2,4), mar=c(4, 4, 1, .3))
attach(all)
plot(S, H, type="p", cex=.75, col="gray")
lines(means$S, means$H, type="l", lwd=2)
plot(S, D, type="p", cex=.75, col="gray")
lines(means$S, means$D, type="l", lwd=2)
plot(S, M, type="p", cex=.75, col="gray")
lines(means$S, means$M, type="l", lwd=2)
plot(S, V, type="p", cex=.75, col="gray")
lines(means$S, means$V, type="l", lwd=2)
plot(S, FRic, type="p", cex=.75, col="gray")
lines(means$S, means$FRic, type="l", lwd=2)
plot(S, FEve, type="p", cex=.75, col="gray")
lines(means$S, means$FEve, type="l", lwd=2)
plot(S, FDiv, type="p", cex=.75, col="gray")
lines(means$S, means$FDiv, type="l", lwd=2)
plot(S, FDis, type="p", cex=.75, col="gray")
lines(means$S, means$FDis, type="l", lwd=2)
par(op)
Run the code above in your browser using DataLab