I am new to loops in R and I need help with writing multiple nested loops. I have a data frame where a row represents counts of species from one site within a region. There are 50 regions, and the number of sites among regions is unequal. For each region I need to calculate a diversity index based on incrementingly increasing the number sites, and replicating this 1000x for each incremental step. For instance:
R1 <- subset(df, region=="1") #this needs to be completed for all 50 regions
R1$region<-NULL
max<-nrow(R1)-1
iter <- 1000 #the number of iterations
n <- 1 # the number of rows to be sampled. This needs to increase until
“max”
outp <- rep(NA, iter)
for (i in 1:iter){
d <- sample(1:nrow(R1), size = n, replace=FALSE)
bootdata <- R1[d,]
x <- colSums(bootdata) #this is not applicable until n>1
outp[i] <- 1/diversity(x, index = "simpson")
}
Here is a sample dataset
structure(list(region = c(1L, 1L, 1L, 2L, 2L, 3L, 4L, 4L), Sp1 = c(31L,
85L, 55L, 71L, 81L, 22L, 78L, 64L), Sp2 = c(10L, 84L, 32L, 86L,
47L, 93L, 55L, 35L), Sp3 = c(86L, 56L, 4L, 8L, 55L, 47L, 51L,
95L)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-8L), .Names = c("region", "Sp1", "Sp2", "Sp3"), spec = structure(list(
cols = structure(list(region = structure(list(), class =
c("collector_integer",
"collector")), Sp1 = structure(list(), class = c("collector_integer",
"collector")), Sp2 = structure(list(), class = c("collector_integer",
"collector")), Sp3 = structure(list(), class = c("collector_integer",
"collector"))), .Names = c("region", "Sp1", "Sp2", "Sp3")),
default = structure(list(), class = c("collector_guess",
"collector"))), .Names = c("cols", "default"), class = "col_spec"))
In short, for each region I need to calculate “simpson’s” index for each site, randomly resampled 1000 times. Then, I need to calculate the index again for 2 sites after each column has been summed, 1000 times. Then 3 sites etc until max.
I also struggle in writing output. I am looking to have one dataframe for each region with columns that represent 1000 iterations of n until max.
Many thanks in advance