I have a dataframe where data are grouped by ID. I need to know how many cells are the 10% of each group in order to select this number in a sample, but this sample should select the cells which EP is 1.

I've tried to do a nested For loop: one For to know the quantity of cells which are the 10% for each group and the bigger one to sample this number meeting the condition EP==1

```
x <- data.frame("ID"=rep(1:2, each=10),"EP" = rep(0:1, times=10))
x
   ID EP
1   1  0
2   1  1
3   1  0
4   1  1
5   1  0
6   1  1
7   1  0
8   1  1
9   1  0
10  1  1
11  2  0
12  2  1
13  2  0
14  2  1
15  2  0
16  2  1
17  2  0
18  2  1
19  2  0
20  2  1

for(j in 1:1000){
  for (i in 1:nrow(x)){
    d <- x[x$ID==i,]
    npix <- 10*nrow(d)/100
  }
  r <- sample(d[d$EP==1,],npix)
  print(r)
}
data frame with 0 columns and 0 rows
data frame with 0 columns and 0 rows
data frame with 0 columns and 0 rows
.
.
.
until 1000

```

I would want to get this dataframe, where each sample is in a new column in x, and the cell sampled has "1":

```
   ID EP s1 s2....s1000
1   1  0 0  0 ....
2   1  1 0  1
3   1  0 0  0
4   1  1 0  0
5   1  0 0  0
6   1  1 0  0
7   1  0 0  0
8   1  1 0  0
9   1  0 0  0
10  1  1 1  0
11  2  0 0  0
12  2  1 0  0
13  2  0 0  0
14  2  1 0  1
15  2  0 0  0
16  2  1 0  0
17  2  0 0  0
18  2  1 1  0
19  2  0 0  0
20  2  1 0  0

```

see that each 1 in S1 and s2 are the sampled cells and correspond to 10% of cells in each group (1, 2) which meet the condition EP==1