[R] binary AND operators in R

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Sun Mar 29 22:06:48 CEST 2009


mauede at alice.it wrote:
> I cannot find any R function or operator that performs a binary AND operation, as performed by Fortran built-in function "iand".
> Ideally either R operator "&" or "&&" should do that. But some tests proved they do not:
>   

they do not, it seems clear from the documentation.  here's a hint:

    RSiteSearch('bitwise')



your question points me to another issue (thanks!):

    one = as.raw(1)
    as.logical(one)
    # TRUE

    two = as.raw(2)
    as.logical(two)
    # TRUE

    one & two
    # 00

    if (one) 1 else 0
    # error: unimplemented type 'raw' in 'asLogical'

oops...

how come 01 AND 02 = 00??  well, you see, & is a *logical* (not a
*bitwise*) AND operator (see ?'&'), but for raws it does bitwise AND
(see ?'&' again):

    as.raw(6) & as.raw(3)
    # 02

?'&' says:

"
Arguments:

    x, y: logical vectors, or objects which can be coerced to such or
          for which methods have been written.
"  

this might have been more explicit about raws, but the use of logical
operators (that is, bitwise operators) with raws *is* described in the
details.

the other case is weird; ?'if' says:

"
Arguments:

    cond: A length-one logical vector that is not 'NA'. Conditions of
          length greater than one are accepted with a warning, but only
          the first element is used.  Other types are coerced to
          logical if possible, ignoring any class.
"

and the help page does not mention 'raw' at all.  raws are not logical:

    is.logical(one)
    # FALSE

(what about the type hierarchy???), yet it is possible to convert them
to logical:

    as.logical(one)
    # TRUE

why if(one) chooses to raise an error is a mystery to me.  the null
hypothesis:  a design flaw.  the alternative one:  a bug.  choose your test.

best,
vQ




More information about the R-help mailing list