Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create unary operator in R

Tags:

r

Is it possible to create a unary operator in R? I know it's possible to create binary operator like this:

setGeneric("%power%", function(x, y) x ^ y)
2 %power% 4

But is it possible to create a unary operator like -. I tried something like:

setGeneric("%-%", function(x) -x)
%-% 3

But it doesn't work

like image 799
eaglefreeman Avatar asked Jan 11 '18 13:01

eaglefreeman


2 Answers

The R parser doesn't support custom unary operators.

A copy of the list of supported operators from the R language definition:

-   Minus, can be unary or binary
+   Plus, can be unary or binary
!   Unary not
~   Tilde, used for model formulae, can be either unary or binary
?   Help
:   Sequence, binary (in model formulae: interaction)
*   Multiplication, binary
/   Division, binary
^   Exponentiation, binary
%x% Special binary operators, x can be replaced by any valid name
%%  Modulus, binary
%/% Integer divide, binary
%*% Matrix product, binary
%o% Outer product, binary
%x% Kronecker product, binary
%in%    Matching operator, binary (in model formulae: nesting)
<   Less than, binary
>   Greater than, binary
==  Equal to, binary
>=  Greater than or equal to, binary
<=  Less than or equal to, binary
&   And, binary, vectorized
&&  And, binary, not vectorized
|   Or, binary, vectorized
||  Or, binary, not vectorized
<-  Left assignment, binary
->  Right assignment, binary
$   List subset, binary

(The parser supports also the binary operator := which is not documented here, because it is not used by base R.)

Note that the only custom operators ("%x% Special binary operators, x can be replaced by any valid name") are binary.

So, your only option is overloading an existing unary operator respectively writing a method for it.

like image 70
Roland Avatar answered Nov 14 '22 22:11

Roland


Although I am not familiar with setGeneric, I can answer the question

Is it possible to create a unary operator in R?

Yes, sort of, but not really. You can fake it:

# LET'S USE A BINARY OPERATOR TO DEFINE A UNARY OPERATOR:
# THE SYMBOL /*/<- IS SUPPOSED TO LOOK LIKE PERCENT-WILDCARD-PERCENT--ASSIGNMENT-ARROW
`%/*/<-%` <- function ( name , FUN , safe = TRUE ) {
    `%//%` <- paste0
    NAME <- "%" %//% name %//% "%"
    PARENT <- parent.frame ()
    if ( safe && exists ( NAME , PARENT ) )
            stop ( NAME %//% " exists." )
    assign (
        x = NAME ,
        value = function ( x , ignored ) FUN ( x ) ,
        envir = PARENT ) }

.. <- NULL            # THIS IS WHAT I MEAN BY FAKING IT...
"-"    %/*/<-%    `-` # ... `%-%` IS ACTUALLY A BINARY OPERATOR....
1 %-%..               # ... IN THIS CALL, `..` IS THE SECOND ARGUMENT.
# [1] -1
1 %-%.. %-%..
# [1] 1

"t"   %/*/<-%   `t` 
m <- matrix(1:4, 2)
m
#      [,1] [,2]
# [1,]    1    3
# [2,]    2    4
m %t%..
#      [,1] [,2]
# [1,]    1    2
# [2,]    3    4

"-"   %/*/<-%   `-` 
# Error in "-" %/*/<-% `-` : %-% exists.

i <- floor ( runif ( 9 , min = 1 , max = 10 ) )
i
# [1] 2 3 2 1 7 3 9 5 9
unique(i)
# [1] 2 3 1 7 9 5
"u"   %/*/<-%   function ( x ) sort ( unique ( x ) )
i %u%..
# [1] 1 2 3 5 7 9
like image 25
Ana Nimbus Avatar answered Nov 14 '22 23:11

Ana Nimbus