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

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


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


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