File: ineq.R

package info (click to toggle)
r-cran-teachingdemos 2.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,168 kB
  • sloc: makefile: 2
file content (65 lines) | stat: -rw-r--r-- 1,561 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#Here are a couple of function definitions that may be more intuitive for some people (see the examples below the function defs).  They are not perfect, but my tests showed they work left to right, right to left, outside in, but not inside out.

`%<%` <- function(x,y) {
        xx <- attr(x,'orig.y')
        yy <- attr(y,'orig.x')

        if(is.null(xx)) {
                xx <- x
                x <- rep(TRUE, length(x))
        }
        if(is.null(yy)) {
                yy <- y
                y <- rep(TRUE, length(y))
        }

        out <- x & y & (xx < yy)
        attr(out, 'orig.x') <- xx
        attr(out, 'orig.y') <- yy

        out
}

`%<=%` <- function(x,y) {
        xx <- attr(x,'orig.y')
        yy <- attr(y,'orig.x')

        if(is.null(xx)) {
                xx <- x
                x <- rep(TRUE, length(x))
        }
        if(is.null(yy)) {
                yy <- y
                y <- rep(TRUE, length(y))
        }

        out <- x & y & (xx <= yy)
        attr(out, 'orig.x') <- xx
        attr(out, 'orig.y') <- yy

        out
}



#
#  x <- -3:3
#
#   -2 %<% x %<% 2
#  c( -2 %<% x %<% 2 )
#  x[ -2 %<% x %<% 2 ]
#  x[ -2 %<=% x %<=% 2 ]
#
#
#  x <- rnorm(100)
#  y <- rnorm(100)
#
#  x[ -1 %<% x %<% 1 ]
#  range( x[ -1 %<% x %<% 1 ] )
#
#
#  cbind(x,y)[ -1 %<% x %<% y %<% 1, ]
#  cbind(x,y)[ (-1 %<% x) %<% (y %<% 1), ]
#  cbind(x,y)[ ((-1 %<% x) %<% y) %<% 1, ]
#  cbind(x,y)[ -1 %<% (x %<% (y %<% 1)), ]
#  cbind(x,y)[ -1 %<% (x %<% y) %<% 1, ] # oops