vendredi 6 août 2021

Correct Use of the "runif" Command (R programming language)

I am using the R programming language. I am trying to make a "self referencing grid" - i.e. a grid with certain conditions. For each row within this grid:

  • var_2 must always be greater than var_1

  • var_4 must always be greater than var_3

  • var_6 must always be greater than var_5

  • var_7 must always be greater than var_6

Here is my own attempt to answer this using the "runif" command:

var_1 <- runif(1000, 0, 100)
var_2 <- runif(1000, var_1, 100)
var_3 <- runif(1000, 0, 100)
var_4 <- runif(1000, var_3, 100)
var_5 <- runif(1000, 0, 100)
var_6 <- runif(1000, var_5, 100)
var_7 <- runif(1000, var_6, 100)

frame_3 <- data.frame(var_1, var_2, var_3, var_4, var_5, var_6, var_7)


head(frame_3)
      var_1    var_2    var_3    var_4     var_5    var_6    var_7
1 16.824184 42.45956 26.68427 41.69108 66.104877 67.29392 92.26807
2  5.154861 74.42316 32.87011 73.93285  3.206936 62.48352 63.06708
3 35.263069 48.56498 34.64460 39.81641 12.816591 13.17151 72.77144
4 21.175502 27.04440 18.04221 64.40537 34.614670 65.89940 92.80989
5 17.156617 86.05252 10.88137 84.53697 86.133523 99.04071 99.55816
6 32.293821 89.77986 26.29747 49.53139 76.262548 81.88121 89.91402

Based on some manual checks, I think this might be correct.

My Question: Is there a "better" way to solve this problem? For instance - can this be done using the "expand.grid" command or with "loops"?

Thanks




Aucun commentaire:

Enregistrer un commentaire