dimanche 12 juin 2022

How to sample across a dataset with two factors in it?

I have a dataframe with two species A and B and certain variables a b associated with the total of 100 rows.

I want to create a sampler such that in one set it randomly picks 6 rows reps from the df dataset. However, the samples for A must only come from rows associated with sp A from df, similarly from B. I want do this for 500 times over for each of species A and B.

I attempted a for loop and when I ran sampling it shows a single row with 6 columns. I would appreciate any guidance

a <- rnorm(100, 2,1)
b <- rnorm(100, 2,1)
sp <- rep(c("A","B"), each = 50)  

df <- data.frame(a,b,sp)

df.sample <- for(i in 1:1000){
             sampling <- sample(df[i,],6,replace = TRUE)
}

#Output in a single row
a     a.1 sp        b sp.1     a.2
1000 1.68951 1.68951  B 1.395995    B 1.68951

#Expected dataframe
df.sample
set rep a b sp
  1  1  1 9  A
  1  2  3 2  A
  1  3  0 2  A
  1  4  1 2  A
  1  5  1 6  A
  1  6  4 2  A
  2  1  1 2  B
  2  2  5 2  B
  2  3  1 2  B
  2  4  1 6  B
  2  5  1 8  B
  2  6  9 2  B
  ....



Aucun commentaire:

Enregistrer un commentaire