mercredi 17 août 2022

Is there a method to sample number of rows randomly while two of those rows are constant

I want to select a certain amount of rows randomly while the first and last samples are always selected. Suppose I have a row of numbers df as

| A  | B |
| -------- | -------------- |
| 1    | 10            |
| 2  | 158            |
| 3   | 106            |
| 4  | 155            |
| 5    | 130            |
| 6  | 154            |
| 7    | 160            |
| 8  | 157            |
| 9    | 140            |
| 10  | 158            |
| 11    | 210            |
| 12 | 157            |
| 13   | 140            |
| 14  | 156            |
| 15    | 160            |
| 16  | 135            |
| 17    | 102            |
| 18  | 150            |
| 19    | 120            |
| 20  | 12         |

From the table, I want to randomly select 5 rows. While selecting 5 rows I want the row 1 and row 20 to be always selected, while the rest of 3 rows can be anything else.

Right now I'm doing the following thing, but don't know if there is a way to do it in the way I want.

n <- 5
shuffled= df[sample(1:nrow(df)), ] #shuffles the entire dataframe
extracted <- shuffled[1:n, ] #extracts top 5 rows from the shuffled sample

I need to do this because I will further analyze the results.




Aucun commentaire:

Enregistrer un commentaire