I've replying to Avi Gross because there is useful information here for the
original poster.
A Fortran compiler is explicitly allowed to transform arithmetic
expressions as if computer
arithmetic obeyed the usual mathematical laws, which it doesn't.  R is not
Fortran.
There are two questions about a built-in function like "+" or "-".
(1) Is it VECTORISED?  That is, is it applied to corresponding elements of
vector arguments.
All the usual arithmetic operations, including comparison, and functions
like "exp" adn
"cos" are vectorised.
(2) is it GENERIC?  That is, can the operation performed vary with the
dynamic type of
one or more arguments?  Again, all the usual arithmetic operations &c are
generic.

The issue here is that because "+" and "-" and unary "-" are generic,
-x + 1 and 1 - x might or might NOT be equivalent, so the R engine is NOT
allowed to
optimise them.  (And this is without worrying about the oddities of IEEE
arithmetic.
The numbers here are all integers, BUT R DOES NOT KNOW THAT until run time.)

By actual measurement, it is clear that
-x + 1 involves *two* operations, a negation and an addition, while
1 - x involves *one* operation, a subtraction.
TWO new vectors are allocated and filled in in the first case,
ONE in the second.

For this particular problem, both versions are too fast for it to matter.
A programmer should, as a rule, write the code SIMPLY and CLEARLY, a metric
that -x + 1 fails.  When the code is working, it it is too slow, it's time
to start measuring and rewriting.  But at the beginning, make it obvious.

On those grounds, 1-x is still a bit tricky.
If there are two values A and B, swapping them can be done by (A+B)-x.
But it doesn't work with three (unless the third is NA, which 1-x leaves
unchanged).
It certainly deserves a comment:
  foobar <- 1 - foobar  # Turn 0 into 1 and vice versa.

Keywords: VECTORISED, GENERIC, and KISS.


On Fri, 30 May 2025 at 2:35 AM, <avi.e.gr...@gmail.com> wrote:

> Mike,
>
> There is in a mathematical sense, no difference between:
>
> 1 - x
>
> and
>
> -x + 1
>
> But for code that is compiled or interpreted, I suspect various strategies
> may make one be acted on a bit differently. Does the hardware for addition
> work differently than for subtraction? Does it implement -x by getting x
> and then in another operation flipping bits to negate it? Or, in some
> cases, if it is recognized you are dealing with very small numbers limited
> to 0 and 1, as can happen if you use a Boolean data type, then operations
> can be avoided entirely or done on single bytes or bits rather than
> full-blown integers and so on.
>
> My previous point was that the solution written as
>
> y<- (-1*x)+1
>
> was a tad too bulky and would have been simpler if there was no use of
> parentheses and no multiplication. Internally -1*x may take more operations
> than -x while your solution may take even less.
>
> Boolean solutions can be more compact as in
>
>  Y <- !X
>
> And, they remove some ambiguities in that many languages allow truth
> values such that 5 is also true and thus the formula fails if it sees a 5
> and changes it to another TRUE value like 4. When there is no reason not
> to, using a Boolean data type is often best for some uses.
>
>
> -----Original Message-----
> From: R-help <r-help-boun...@r-project.org> On Behalf Of Mike Day via
> R-help
> Sent: Thursday, May 29, 2025 3:45 AM
> To: R help Mailing list <r-help@r-project.org>
> Subject: Re: [R] Looking for a function or a set of steps
>
> What's wrong with
>    1-x
> ?
>
> Sent from my iPad
>
> > On 28 May 2025, at 21:41, Avi Gross <avi.e.gr...@gmail.com> wrote:
> >
> > Paul,
> >
> > Perhaps slightly better and more concise is
> >
> > y <- -x + 1
> >
> > Why multiply? Of course it may be optimized in some cases.
> >
> >> On Tue, May 27, 2025, 3:36 AM Paul Zachos <p...@acase.org> wrote:
> >>
> >> Wow! Amazing stuff.
> >> It will take me a while to digest all that you have offered here.
> >>
> >> I came up with a simple solution myself:
> >> y<- (-1*x)+1
> >>
> >> Thank you
> >> _________________
> >> Paul Zachos, PhD
> >> Director, Research and Evaluation
> >> Association for the Cooperative Advancement of Science and Education
> >> (ACASE)
> >> 110 Spring Street  Saratoga Springs, NY 12866  |
> >> p...@acase.org  |  www.acase.org
> >>
> >>
> >>
> >>
> >>>> On May 19, 2025, at 3:08 AM, Rui Barradas <ruipbarra...@sapo.pt>
> wrote:
> >>>
> >>> Às 18:40 de 18/05/2025, paul zachos via R-help escreveu:
> >>>> Dear R Community
> >>>> I am an R beginner
> >>>> I have a vector of ‘1’s and ‘0’s
> >>>> x
> >>>> [1] 0 0 1 0 0 1 1 0 1 0 1 1 1 0 1 1 0 0 1 0 1 0 1 0 0 0 0
> >>>> [28] 0 1 1 0 1 0 1 1 0 0 0 0 1 0 1 1 0 0 1 0 0 1 0 0 0 1 1
> >>>> [55] 0 0 1 0 1 0 0 0 1 1 1 1 0 1 0 0 0 1 0 0 1 1 0 0 0 0 0
> >>>> [82] 1 1 1 0 1 0 0 1 0 0 1 1 1 1 1
> >>>> I would like to generate a new vector  in which the ‘1’s in x become
> >> ‘0’s and the ‘0’s in x become ‘1’s.
> >>>> How should I go about this?
> >>>> Thank you,
> >>>> paz
> >>>> ______________________________________________
> >>>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >>>> https://stat.ethz.ch/mailman/listinfo/r-help
> >>>> PLEASE do read the posting guide
> >> https://www.R-project.org/posting-guide.html
> >>>> and provide commented, minimal, self-contained, reproducible code.
> >>> Hello,
> >>>
> >>> A simple way is to treat x as logical and negate its values. Then
> coerce
> >> to integer.
> >>>
> >>>
> >>> x <- c(0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 1L,
> >>> 1L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L,
> >>> 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 0L,
> >>> 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 1L,
> >>> 1L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L,
> >>> 0L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 1L,
> >>> 1L)
> >>>
> >>>
> >>> as.integer(!x)
> >>> #>  [1] 1 1 0 1 1 0 0 1 0 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 1 1 1 0 0 1 0
> >> 1 0 0 1 1 1
> >>> #> [39] 1 0 1 0 0 1 1 0 1 1 0 1 1 1 0 0 1 1 0 1 0 1 1 1 0 0 0 0 1 0 1 1
> >> 1 0 1 1 0 0
> >>> #> [77] 1 1 1 1 1 0 0 0 1 0 1 1 0 1 1 0 0 0 0 0
> >>>
> >>>
> >>> Also, the recommended way of posting data is with ?dput:
> >>>
> >>>
> >>> dput(x)
> >>> #> c(0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 1L,
> >>> #> 1L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L,
> >>> #> 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 0L,
> >>> #> 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 1L,
> >>> #> 1L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L,
> >>> #> 0L, 0L, 1L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 1L, 1L,
> >>> #> 1L)
> >>>
> >>>
> >>> Hope this helps,
> >>>
> >>> Rui Barradas
> >>>
> >>>
> >>> --
> >>> Este e-mail foi analisado pelo software antivírus AVG para verificar a
> >> presença de vírus.
> >>> www.avg.com
> >>
> >>
> >>        [[alternative HTML version deleted]]
> >>
> >> ______________________________________________
> >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide
> >> https://www.R-project.org/posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >
> >    [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> https://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to