Creating Tables With Certain Conditions

I am trying to make "self-referencing" grids using the R programming language. For instance, I made the following (non self-referencing) grid:

var_1 <- rnorm(1000,8,5)
var_2 <- rnorm(1000,8,5)
var_3 <- rnorm(1000,8,5)
var_4 <- rnorm(1000,8,5)
var_5 <- rnorm(1000,8,5)
var_6 <- rnorm(1000,8,5)
var_7 <- rnorm(1000,8,5)

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

This works:

head(frame_a)
      var_1     var_2     var_3     var_4     var_5      var_6     var_7
1 11.106978  6.045635 12.059918 14.576642 6.6631061  2.4308557  4.210330
2  5.356593  9.066030 15.324603  4.170090 7.9821328  0.7412383  8.561553
3 16.971664  5.172987  6.501061  2.827789 0.5317087  6.2078346  9.151058
4  1.903000  7.183025 11.524599 12.188365 9.2445369  9.5063593  3.741548
5 11.744789 10.077375  4.740652  3.738349 6.1231457  5.3983903 -4.863340
6 14.151094  8.234436  8.203216 14.722724 9.6634154 -0.7918751 14.080428

Now, 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

This was my attempt to make such a grid

var_1 <- rnorm(1000,8,5)
var_2 <- rnorm(1000,var_1,5)
var_3 <- rnorm(1000,8,5)
var_4 <- rnorm(1000,var_3,5)
var_5 <- rnorm(1000,6,5)
var_6 <- rnorm(1000,var_5,5)
var_7 <- rnorm(1000,var_6,5)

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

This code worked, but the "conditions" were not respected:

> head(frame_b)
       var_1     var_2    var_3     var_4     var_5       var_6      var_7
1  5.9667356  1.518180 9.316051  3.192931 -1.358661 -0.05681164 -5.3893074
2  8.4478170 11.902876 9.588589 -1.220872  6.091870  9.81611979  4.9043055
3 13.6396693  8.554241 9.017625 10.840609 -4.548760  0.22775771 -5.8313070
4 13.4776003 19.735430 4.674371  2.144886  4.933787 -3.13381274 -0.5747767
5  6.3300270 -1.968539 4.413483  5.490492  4.866916  3.72772243 10.1688481
6  0.3662306 -1.633712 6.142054  7.236065 12.506673 10.17450918  7.7536291

For instance, in the first row: var_2 is not greater than var_1.

Can someone please show me how to fix this error?

Thanks

Here is my own attempt to answer this:

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

Is this correct?

Thanks

You are using random distributions, so due to the variation, they won't always be greater than in another column.

1 Like

thank you for your reply!

do you think that my attempt to answer my own question is correct?

thanks!

It seems to work when you look at the output from pairs(frame_3)

1 Like

wow, this is pretty cool! But I am not too sure what is the significance of this step (i.e. pairs(frame_3)) and why it is important?

thanks!

It is just a quick way of visualising the variables. So you when you look at the plot from var1 and var2 you can see that all the points are above the diagonal.

When you look at summary(frame_3) it looks like it shouldn't make sense. You pass a full vector in to the second argument (min) of runif()which you probably shouldn't do, but it works.

1 Like

thank you for your reply!

what do you think of my answer in the end? was this a good way to solve this problem? or should have i approached this problem in a different way?

thanks!

Look, it works, but someone else could probably give you a better answer.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.