I want to carry out multistage random sampling on a master dataframe, in order to compare results of different sample sizes. Different examples that I have seen on stackoverflow have carried out sampling based on number of rows, but that is not exactly the expectation in this task. Overview of the master dataframe is this:
glimpse(df)
Rows: 23,404
Columns: 4
$ variety <dbl> 330, 330, 330, 330, 330, 330, 251, 251, 251, 251, 251, 251, 312, 312, 312, 312, 312, 312, 312, 31...
$ plantno <dbl> 13, 13, 13, 13, 13, 13, 19, 19, 19, 19, 19, 19, 23, 23, 23, 23, 23, 23, 23, 23, 29, 29, 29, 29, 2...
$ point <chr> "L.0000", "L.0001", "R.0000", "R.0001", "R.0002", "R.0003", "L.0000", "L.0001", "L.0002", "R.0000...
$ length <dbl> 31.9868410, 25.7816640, 18.4944923, 19.5613560, 10.7766413, 10.1496060, 10.7168411, 8.5189431, 10...
The sampling are done in two stages:
- 1st stage - sample plants per variety (this is not equivalent to rows). Sample sizes are
5 10 15 20 25 30 35. For instance, sampling5plants per variety will not mean sampling five rows but sampling unique fiveplantno, which in real sense goes along with their replicates. The picture below shows a correct example of sampling after stage 1. It shows that under variety 201, five plants15 10 18 17 36fromplantnocolumn were sampled.
- 2nd stage - sample
pointperplantno. Sample sizes are2 4 6 8 10. The picture below shows a correct example of sampling after stage 2. For example, sampling2points perplantnoshows that undervariety201,plantno10 & 15 were sampled from the 1st stage, while for the 2nd stage,L.0002&R.0004were sampled fromplantno10, andL.0002sampled fromplantno15. Stage 2 is actually the final result. Each sampling cycle is stored in a list which are later joined together.
The attempt I made according to the code below did not give me expected result. The result seem to be filled with duplicates, and not according to the table shown above. The . Any help will be appreciated.
library(purrr)
library(tidyr)
library(rio)
library(tidyverse)
# import data from the web to your local computer
myurl <- "https://drive.google.com/file/d/1ym_s5KeqcQ9yZFBK2nO6FGhC_WYVUv8x/view?usp=sharing"
download.file(url=myurl, destfile="df_test1.xlsx", mode="wb")
# import multiple excel workbook using rio library
df <- import_list(file.choose(), setclass = "tbl")
df <- df[[1]]
glimpse(df)
## generate sequence of sample sizes for tree per family
J <- seq(5,35,5)
# l1 <- vector(length = length(J), mode = "list")
## generate sequence of points per tree
k <- seq(2,10,2)
l2 <- vector(length = length(k), mode = "list")
l3 <- vector(length = length(J), mode = "list")
for (i in seq_along(J)){ # you know of seq_along() right?
l1 = df %>%
group_by(plantno, variety) %>%
# distinct %>%
sample_n(J[i], replace = T)
#
# l1 <- l1 %>% left_join(df)
sampleSize_variety <- rep(J[i], nrow(l1))
l1$sampleSize_variety <- sampleSize_variety
for (j in seq_along(k)){
l4 = l1 %>%
group_by(variety, plantno) %>%
sample_n(k[j], replace = T)
sampleSize_pointPerPlant <- rep(k[j], nrow(l4))
l4$sampleSize_pointPerPlant <- sampleSize_pointPerPlant
l2[[j]] = l4
}
l3[[i]] <- l2
}
# bind tables together - tables are in nested list
merge_table <- transpose(l3) %>% map(bind_rows)
merge_table1 <- bind_rows(merge_table)
merge_table1


Aucun commentaire:
Enregistrer un commentaire