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
?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 at 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
1-x> On 18.05.2025, at 19:40, paul zachos via R-help <r-help at r-project.org> wrote: > > 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 at 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.
On Sun, 18 May 2025 13:40:31 -0400 paul zachos via R-help <r-help at r-project.org> wrote:> 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?Looks suspiciously like a homework question to me. cheers, Rolf Turner -- Honorary Research Fellow Department of Statistics University of Auckland Stats. Dep't. (secretaries) phone: +64-9-373-7599 ext. 89622 Home phone: +64-9-480-4619
The obvious answer is 1-x. Another answer, which is good for converting any 0..n elements to any others, is c(1,0)[x+1]. But the most important questions are - where did x come from? - what does it mean? - why do you want to do this? - what do you already know about R? Do you understand vectorisation? Do you understand subscripts? could you have thought of ifelse(x == 0, 1, 0)? - did you have any idea of your own, and if so what? - what *exactly* are you asking for? On Mon, 19 May 2025 at 18:20, paul zachos via R-help <r-help at r-project.org> wrote:> > 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 at 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.