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.