0

i'm trying to find which simple events have a difference between the maximum and minimum of no more than 10. the code i have is

sampleSpace <- combinayions(100,3,seq(1,100,by=1),repeats.allowed=FALSE)


diff <- sample_max - sample_min 

can anyone help me.

mei
  • 35
  • 8

3 Answers3

0

The subset of sampleSpace where the difference between max and min in each row is given by:

ss <- apply(sampleSpace, 1, function(x) abs(diff(range(x))) <= 10)

So you can do

sampleSpace[ss,]

to get just the rows meeting that criterion. There are 25,020 remaining rows. The first 30 are:

sampleSpace[ss,]
#>          [,1] [,2] [,3]
#>     [1,]    1    2    3
#>     [2,]    1    2    4
#>     [3,]    1    2    5
#>     [4,]    1    2    6
#>     [5,]    1    2    7
#>     [6,]    1    2    8
#>     [7,]    1    2    9
#>     [8,]    1    2   10
#>     [9,]    1    2   11
#>    [10,]    1    3    2
#>    [11,]    1    3    4
#>    [12,]    1    3    5
#>    [13,]    1    3    6
#>    [14,]    1    3    7
#>    [15,]    1    3    8
#>    [16,]    1    3    9
#>    [17,]    1    3   10
#>    [18,]    1    3   11
#>    [19,]    1    4    2
#>    [20,]    1    4    3
#>    [21,]    1    4    5
#>    [22,]    1    4    6
#>    [23,]    1    4    7
#>    [24,]    1    4    8
#>    [25,]    1    4    9
#>    [26,]    1    4   10
#>    [27,]    1    4   11
#>    [28,]    1    5    2
#>    [29,]    1    5    3
#>    [30,]    1    5    4
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
0

you can simply use which(), as follows:

sampleSpace = sampleSpace[which(diff <= 10), ]
head(sampleSpace)

Here is the output:

     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    1    2    4
[3,]    1    2    5
[4,]    1    2    6
[5,]    1    2    7
[6,]    1    2    8
rodolfoksveiga
  • 1,181
  • 4
  • 17
0

We can use rowRanges from matrixStats

library(matrixStats)
m1 <- rowRanges(sampleSpace)
out <- sampleSpace[(m1[,2]- m1[,1]) <= 10,]
akrun
  • 874,273
  • 37
  • 540
  • 662