I have a forest inventory dataset of trees from 89 plots with different ground areas (0.16ha, 1ha, 2.5ha etc). To make the data suitable for my analysis, I have to extrapolate the population of the larger plots with reference to the smallest plot (0.16ha) and then randomly sample elements from the larger plots to make up the expected population (Population of the Large plot * area of smallest plot/area of the large plot). In doing this, each of the larger plots would have a different expected population.
I can randomly select a single size from all plots in the list but I having difficulty modifying my code to randomly select a different size from the plots in the list. For example, in the data provided click here, I have successfully developed the code below to randomly sample 5 elements from each plot in the list: data_split
.
# Import tree data
species_data <- read.csv("stackoverflow_help.csv", header = TRUE, stringsAsFactors = FALSE)
# Split the dataset into a list of plots
data_split <- split( species_data , f = species_data$Plot)
# creating a loop to randomly select 5 individuals from the each plot (without replacing after selection)
library(dplyr)
fun2<- function (filename) {
sample_n(filename, size=5, replace=F)
}
# apply the loop
sorting <- lapply(data_split, fun2)
# result from the random sampling
How can the size=5 in the loop be modified to size = sizes <- c(4, 6, 8, 4, 10) and applied to each plot in the list? So 4, 6, 8, 4, and 10 elements will be sampled from L2P1, L2P2, L2P3, L3P1 and L3P2 respectively.
