Skip to contents

Comparison operators that can be chained together into something like 0 %<% x %<% 1 instead of 0 < x && x < 1.

Usage

x %<% y
x %<=% y

Arguments

x,y

Values to compare

Details

These functions/operators allow chained inequalities. To specify that you want the values between two values (say 0 and 1) you can use 0 %<% x %<% 1 rather than 0 < x \&\& x < 1 .

Value

A logical vector is returned that can be used for subsetting like <, but the original values are included as attributes to be used in additional comparisons.

Author

Greg Snow, 538280@gmail.com

Note

This operator is not fully associative and has different precedence than < and <=, so be careful with parentheses. See the examples.

Examples


x <- -3:3

 -2 %<% x %<% 2
#> [1] FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE
#> attr(,"orig.x")
#> [1] -3 -2 -1  0  1  2  3
#> attr(,"orig.y")
#> [1] 2
c( -2 %<% x %<% 2 )
#> [1] FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE
x[ -2 %<% x %<% 2 ]
#> [1] -1  0  1
x[ -2 %<=% x %<=% 2 ]
#> [1] -2 -1  0  1  2


x <- rnorm(100)
y <- rnorm(100)

x[ -1 %<% x %<% 1 ]
#>  [1] -0.229135339  0.431959907  0.015625609  0.139813862 -0.273222131
#>  [6] -0.990340119  0.446491228 -0.919854722  0.153760197 -0.040239407
#> [11]  0.567338547  0.042799088 -0.965960474 -0.760014817 -0.438618978
#> [16]  0.106089306 -0.250500817 -0.842435104 -0.424886665 -0.125745121
#> [21] -0.562377813 -0.307882505  0.008784695  0.538543157 -0.995322644
#> [26] -0.350760193 -0.447175895 -0.978851527  0.163379202  0.686010058
#> [31]  0.337546016 -0.222942046  0.002176707 -0.028092872 -0.461639762
#> [36] -0.822360392  0.095417294 -0.751540872  0.463427820  0.883342436
#> [41] -0.222891730  0.619504556  0.082115936  0.483929222  0.594113546
#> [46]  0.216827755  0.544789838 -0.081743918  0.339195837  0.451740335
#> [51]  0.647599596  0.168694563 -0.381843002 -0.882540483 -0.844059717
#> [56]  0.592512769 -0.971294782 -0.643273927 -0.574786476 -0.222551829
#> [61] -0.242395953 -0.805510580  0.039382701 -0.487260745 -0.341582122
#> [66]  0.495971790  0.764205661  0.364760065  0.524550273 -0.626334292
#> [71] -0.491154533
range( x[ -1 %<% x %<% 1 ] )
#> [1] -0.9953226  0.8833424


cbind(x,y)[ -1 %<% x %<% y %<% 1, ]
#>                 x           y
#>  [1,] -0.22913534 -0.19125192
#>  [2,] -0.27322213  0.03487170
#>  [3,] -0.91985472 -0.51760162
#>  [4,] -0.04023941  0.17805230
#>  [5,]  0.04279909  0.64581898
#>  [6,] -0.96596047 -0.34429989
#>  [7,]  0.10608931  0.64942234
#>  [8,] -0.25050082 -0.23094619
#>  [9,] -0.84243510  0.42041635
#> [10,] -0.12574512  0.06831662
#> [11,] -0.56237781 -0.22822100
#> [12,] -0.30788251  0.97052875
#> [13,] -0.99532264  0.35407460
#> [14,] -0.44717590  0.35767220
#> [15,]  0.33754602  0.55630290
#> [16,] -0.22294205  0.16523621
#> [17,]  0.46342782  0.87060302
#> [18,] -0.22289173  0.76088058
#> [19,]  0.08211594  0.94179718
#> [20,]  0.33919584  0.73593829
#> [21,] -0.38184300  0.61979043
#> [22,] -0.97129478 -0.92231807
#> [23,] -0.64327393  0.80940472
#> [24,] -0.57478648  0.85131943
#> [25,] -0.24239595  0.10979299
#> [26,] -0.80551058  0.37015108
#> [27,]  0.03938270  0.80449641
#> [28,]  0.49597179  0.94801674
#> [29,]  0.52455027  0.92179900
#> [30,] -0.62633429 -0.04097913
#> [31,] -0.49115453  0.36178968
cbind(x,y)[ (-1 %<% x) %<% (y %<% 1), ]
#>                 x           y
#>  [1,] -0.22913534 -0.19125192
#>  [2,] -0.27322213  0.03487170
#>  [3,] -0.91985472 -0.51760162
#>  [4,] -0.04023941  0.17805230
#>  [5,]  0.04279909  0.64581898
#>  [6,] -0.96596047 -0.34429989
#>  [7,]  0.10608931  0.64942234
#>  [8,] -0.25050082 -0.23094619
#>  [9,] -0.84243510  0.42041635
#> [10,] -0.12574512  0.06831662
#> [11,] -0.56237781 -0.22822100
#> [12,] -0.30788251  0.97052875
#> [13,] -0.99532264  0.35407460
#> [14,] -0.44717590  0.35767220
#> [15,]  0.33754602  0.55630290
#> [16,] -0.22294205  0.16523621
#> [17,]  0.46342782  0.87060302
#> [18,] -0.22289173  0.76088058
#> [19,]  0.08211594  0.94179718
#> [20,]  0.33919584  0.73593829
#> [21,] -0.38184300  0.61979043
#> [22,] -0.97129478 -0.92231807
#> [23,] -0.64327393  0.80940472
#> [24,] -0.57478648  0.85131943
#> [25,] -0.24239595  0.10979299
#> [26,] -0.80551058  0.37015108
#> [27,]  0.03938270  0.80449641
#> [28,]  0.49597179  0.94801674
#> [29,]  0.52455027  0.92179900
#> [30,] -0.62633429 -0.04097913
#> [31,] -0.49115453  0.36178968
cbind(x,y)[ ((-1 %<% x) %<% y) %<% 1, ]
#>                 x           y
#>  [1,] -0.22913534 -0.19125192
#>  [2,] -0.27322213  0.03487170
#>  [3,] -0.91985472 -0.51760162
#>  [4,] -0.04023941  0.17805230
#>  [5,]  0.04279909  0.64581898
#>  [6,] -0.96596047 -0.34429989
#>  [7,]  0.10608931  0.64942234
#>  [8,] -0.25050082 -0.23094619
#>  [9,] -0.84243510  0.42041635
#> [10,] -0.12574512  0.06831662
#> [11,] -0.56237781 -0.22822100
#> [12,] -0.30788251  0.97052875
#> [13,] -0.99532264  0.35407460
#> [14,] -0.44717590  0.35767220
#> [15,]  0.33754602  0.55630290
#> [16,] -0.22294205  0.16523621
#> [17,]  0.46342782  0.87060302
#> [18,] -0.22289173  0.76088058
#> [19,]  0.08211594  0.94179718
#> [20,]  0.33919584  0.73593829
#> [21,] -0.38184300  0.61979043
#> [22,] -0.97129478 -0.92231807
#> [23,] -0.64327393  0.80940472
#> [24,] -0.57478648  0.85131943
#> [25,] -0.24239595  0.10979299
#> [26,] -0.80551058  0.37015108
#> [27,]  0.03938270  0.80449641
#> [28,]  0.49597179  0.94801674
#> [29,]  0.52455027  0.92179900
#> [30,] -0.62633429 -0.04097913
#> [31,] -0.49115453  0.36178968
cbind(x,y)[ -1 %<% (x %<% (y %<% 1)), ]
#>                 x           y
#>  [1,] -0.22913534 -0.19125192
#>  [2,] -0.27322213  0.03487170
#>  [3,] -0.91985472 -0.51760162
#>  [4,] -0.04023941  0.17805230
#>  [5,]  0.04279909  0.64581898
#>  [6,] -0.96596047 -0.34429989
#>  [7,]  0.10608931  0.64942234
#>  [8,] -0.25050082 -0.23094619
#>  [9,] -0.84243510  0.42041635
#> [10,] -0.12574512  0.06831662
#> [11,] -0.56237781 -0.22822100
#> [12,] -0.30788251  0.97052875
#> [13,] -0.99532264  0.35407460
#> [14,] -0.44717590  0.35767220
#> [15,]  0.33754602  0.55630290
#> [16,] -0.22294205  0.16523621
#> [17,]  0.46342782  0.87060302
#> [18,] -0.22289173  0.76088058
#> [19,]  0.08211594  0.94179718
#> [20,]  0.33919584  0.73593829
#> [21,] -0.38184300  0.61979043
#> [22,] -0.97129478 -0.92231807
#> [23,] -0.64327393  0.80940472
#> [24,] -0.57478648  0.85131943
#> [25,] -0.24239595  0.10979299
#> [26,] -0.80551058  0.37015108
#> [27,]  0.03938270  0.80449641
#> [28,]  0.49597179  0.94801674
#> [29,]  0.52455027  0.92179900
#> [30,] -0.62633429 -0.04097913
#> [31,] -0.49115453  0.36178968
cbind(x,y)[ -1 %<% (x %<% y) %<% 1, ] # oops
#>                 x           y
#>  [1,] -0.22913534 -0.19125192
#>  [2,] -0.27322213  0.03487170
#>  [3,] -0.91985472 -0.51760162
#>  [4,] -0.04023941  0.17805230
#>  [5,]  0.04279909  0.64581898
#>  [6,] -0.96596047 -0.34429989
#>  [7,] -0.76001482  1.11532059
#>  [8,]  0.10608931  0.64942234
#>  [9,] -0.25050082 -0.23094619
#> [10,] -0.84243510  0.42041635
#> [11,] -0.12574512  0.06831662
#> [12,] -0.56237781 -0.22822100
#> [13,] -0.30788251  0.97052875
#> [14,] -0.99532264  0.35407460
#> [15,] -0.44717590  0.35767220
#> [16,]  0.33754602  0.55630290
#> [17,] -0.22294205  0.16523621
#> [18,] -0.46163976  3.65639170
#> [19,]  0.46342782  0.87060302
#> [20,]  0.88334244  1.01737095
#> [21,] -0.22289173  0.76088058
#> [22,]  0.08211594  0.94179718
#> [23,]  0.59411355  1.40612075
#> [24,]  0.54478984  1.15272588
#> [25,]  0.33919584  0.73593829
#> [26,] -0.38184300  0.61979043
#> [27,] -0.88254048  1.44019082
#> [28,] -0.84405972  1.18043891
#> [29,] -0.97129478 -0.92231807
#> [30,] -0.64327393  0.80940472
#> [31,] -0.57478648  0.85131943
#> [32,] -0.24239595  0.10979299
#> [33,] -0.80551058  0.37015108
#> [34,]  0.03938270  0.80449641
#> [35,] -0.48726075  1.15831763
#> [36,]  0.49597179  0.94801674
#> [37,]  0.52455027  0.92179900
#> [38,] -0.62633429 -0.04097913
#> [39,] -0.49115453  0.36178968


3 
#> [1] 3
3 
#> [1] 3