I am working with the R programming language.
I would like to generate random numbers : a1, a2, a3, b1, b2, b3
I would like there to be a condition such that:
- a3 > a2 > a1
- b3 > b2 > b1
I do not know how to do this directly, so I tried to generate a large data frame of numbers and only keep rows that match this condition:
a1 = rnorm(100000,10,10)
a2 = rnorm(100000,10,10)
a3 = rnorm(100000,10,10)
b1 = rnorm(100000,10,10)
b2 = rnorm(100000,10,10)
b3 = rnorm(100000,10,10)
my_data = data.frame(a1, a2, a3, b1, b2,b3)
This data frame looks like this:
head(my_data)
a1 a2 a3 b1 b2 b3
1 5.6713342 -4.5930442 6.063861 28.9258586 -1.073999 23.7398862
2 17.5791993 5.1482061 6.683438 9.2969640 6.438304 10.2569026
3 13.9389949 8.9943351 1.089840 12.9340164 22.099974 -0.6791567
4 16.0257008 10.4139726 18.469092 10.9470812 20.105047 0.4710750
5 -0.1370202 0.9112077 4.349729 11.9442915 22.318155 8.7671923
6 18.8508432 -3.6210024 3.022941 0.6319464 14.406452 25.2002712
I then tried to make an "indicator" variable that indicates whether a row should be deleted or kept based on whether or not it matches the conditions:
my_data$indicator_a2_a1 = ifelse(my_data$a2 > my_data$a1, "TRUE", "FALSE")
my_data$indicator_a3_a2 = ifelse(my_data$a3 > my_data$a2, "TRUE", "FALSE")
my_data$indicator_a3_a1 = ifelse(my_data$a3 > my_data$a1, "TRUE", "FALSE")
my_data$indicator_b2_b1 = ifelse(my_data$b2 > my_data$b1, "TRUE", "FALSE")
my_data$indicator_b3_b2 = ifelse(my_data$b3 > my_data$b2, "TRUE", "FALSE")
my_data$indicator_b3_b1 = ifelse(my_data$b3 > my_data$b1, "TRUE", "FALSE")
With these indicators, the data now looks like this:
a1 a2 a3 b1 b2 b3 indicator_a2_a1 indicator_a3_a2 indicator_a3_a1 indicator_b2_b1 indicator_b3_b2 indicator_b3_b1
1 5.6713342 -4.5930442 6.063861 28.9258586 -1.073999 23.7398862 FALSE TRUE TRUE FALSE TRUE FALSE
2 17.5791993 5.1482061 6.683438 9.2969640 6.438304 10.2569026 FALSE TRUE FALSE FALSE TRUE TRUE
3 13.9389949 8.9943351 1.089840 12.9340164 22.099974 -0.6791567 FALSE FALSE FALSE TRUE FALSE FALSE
4 16.0257008 10.4139726 18.469092 10.9470812 20.105047 0.4710750 FALSE TRUE TRUE TRUE FALSE FALSE
5 -0.1370202 0.9112077 4.349729 11.9442915 22.318155 8.7671923 TRUE TRUE TRUE TRUE FALSE FALSE
6 18.8508432 -3.6210024 3.022941 0.6319464 14.406452 25.2002712 FALSE TRUE FALSE TRUE TRUE TRUE
Finally, I isolated rows in which all indicators were TRUE:
final_file <- my_data[which(my_data$indicator_a2_a1 == "TRUE" & my_data$indicator_a3_a2 == "TRUE" & my_data$indicator_a3_a1 == "TRUE" & my_data$indicator_b2_b1 == "TRUE" & my_data$indicator_b3_b2 == "TRUE" & my_data$indicator_b3_b1 == "TRUE"), ]
dim(final_file)
[1] 2754 12
This was successfully accomplished the task - but I was wondering if there is a more "efficient" way to perform this task. For example, I randomly generated 100000 rows, but only 2754 of these rows (~ 2%) met the condition I had wanted. The other problem is that I had to manually create 6 indicator variables to make sure all conditions were respected - had there been more conditions, I would have been required to manually create a large number of indicator variables to ensure that all the conditions were respected.
My Question: Is there a way to randomly generate data according to some conditions such that ALL rows produced would meet these conditions? Could this be done with a WHILE LOOP?
I would like to tell the computer: Generate 1000 rows of random data in which:
- a3 > a2 > a1
- b3 > b2 > b1
Can this be done directly?
Thanks!