?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
Surely doing y <- 1 - x would be the simplest way? Best, Tom> -----Original Message----- > From: R-help <r-help-bounces at r-project.org> On Behalf Of Rui Barradas > Sent: 19 May 2025 08:08 > To: paul zachos <anthanasios at me.com>; r-help at r-project.org > Subject: Re: [R] Looking for a function or a set of steps > > ?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://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-he > > > lp__;!!IhKztkE!ZOgMEg6niCyC5iRHP37pMsbvSVBplGd5tceX7MuEFbAIlEIUCve > Qks6 > > n0DSai8s8sL6SEi_ShbQiY5HcgDz-iNeh4gbw$ > > PLEASE do read the posting guide > > https://urldefense.com/v3/__https://www.R-project.org/posting-guide.ht > > > ml__;!!IhKztkE!ZOgMEg6niCyC5iRHP37pMsbvSVBplGd5tceX7MuEFbAIlEIUCve > Qks6 > > n0DSai8s8sL6SEi_ShbQiY5HcgDz-iJzObndg$ > > 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. > https://urldefense.com/v3/__http://www.avg.com__;!!IhKztkE!ZOgMEg6niCy > C5iRHP37pMsbvSVBplGd5tceX7MuEFbAIlEIUCveQks6n0DSai8s8sL6SEi_ShbQi > Y5HcgDz-iB9H6CAC$ > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r- > help__;!!IhKztkE!ZOgMEg6niCyC5iRHP37pMsbvSVBplGd5tceX7MuEFbAIlEIUC > veQks6n0DSai8s8sL6SEi_ShbQiY5HcgDz-iNeh4gbw$ > PLEASE do read the posting guide > https://urldefense.com/v3/__https://www.R-project.org/posting- > guide.html__;!!IhKztkE!ZOgMEg6niCyC5iRHP37pMsbvSVBplGd5tceX7MuEFbAI > lEIUCveQks6n0DSai8s8sL6SEi_ShbQiY5HcgDz-iJzObndg$ > and provide commented, minimal, self-contained, reproducible code.________________________________ Important Notice: Liverpool John Moores University was established as a Higher Education Corporation under section 121 of the Education Reform Act 1988. Further information about Liverpool John Moores University can be found at https://www.ljmu.ac.uk/about-us The information in this email and any attachments is for the sole use of the intended recipient(s). If you are not an intended recipient, or a person responsible for delivering it to an intended recipient, you should delete it from your system immediately without disclosing its contents elsewhere and advise the sender by returning the email or by telephoning a number contained in the body of the email. No responsibility is accepted for loss or damage arising from viruses or changes made to this message after it was sent and the recipient must ensure that the email (and attachments) are virus free. The views contained in this email are those of the author and not necessarily those of Liverpool John Moores University. We will use the personal data information provided by you to respond to your email. For information about how we process personal data and monitor communications please see our Privacy Notice. https://www.ljmu.ac.uk/legal/privacy-and-cookies
Hello, You are right, I didn't think about that one. Here is another, esoteric, way. With `+` substituting for as.integer: +!x Hope this helps, Rui Barradas ?s 09:10 de 19/05/2025, Goodale, Tom escreveu:> Surely doing > > y <- 1 - x > > would be the simplest way? > > Best, > Tom > >> -----Original Message----- >> From: R-help <r-help-bounces at r-project.org> On Behalf Of Rui Barradas >> Sent: 19 May 2025 08:08 >> To: paul zachos <anthanasios at me.com>; r-help at r-project.org >> Subject: Re: [R] Looking for a function or a set of steps >> >> ?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://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r-he >>> >> lp__;!!IhKztkE!ZOgMEg6niCyC5iRHP37pMsbvSVBplGd5tceX7MuEFbAIlEIUCve >> Qks6 >>> n0DSai8s8sL6SEi_ShbQiY5HcgDz-iNeh4gbw$ >>> PLEASE do read the posting guide >>> https://urldefense.com/v3/__https://www.R-project.org/posting-guide.ht >>> >> ml__;!!IhKztkE!ZOgMEg6niCyC5iRHP37pMsbvSVBplGd5tceX7MuEFbAIlEIUCve >> Qks6 >>> n0DSai8s8sL6SEi_ShbQiY5HcgDz-iJzObndg$ >>> 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. >> https://urldefense.com/v3/__http://www.avg.com__;!!IhKztkE!ZOgMEg6niCy >> C5iRHP37pMsbvSVBplGd5tceX7MuEFbAIlEIUCveQks6n0DSai8s8sL6SEi_ShbQi >> Y5HcgDz-iB9H6CAC$ >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://urldefense.com/v3/__https://stat.ethz.ch/mailman/listinfo/r- >> help__;!!IhKztkE!ZOgMEg6niCyC5iRHP37pMsbvSVBplGd5tceX7MuEFbAIlEIUC >> veQks6n0DSai8s8sL6SEi_ShbQiY5HcgDz-iNeh4gbw$ >> PLEASE do read the posting guide >> https://urldefense.com/v3/__https://www.R-project.org/posting- >> guide.html__;!!IhKztkE!ZOgMEg6niCyC5iRHP37pMsbvSVBplGd5tceX7MuEFbAI >> lEIUCveQks6n0DSai8s8sL6SEi_ShbQiY5HcgDz-iJzObndg$ >> and provide commented, minimal, self-contained, reproducible code. > > ________________________________ > Important Notice: Liverpool John Moores University was established as a Higher Education Corporation under section 121 of the Education Reform Act 1988. Further information about Liverpool John Moores University can be found at https://www.ljmu.ac.uk/about-us The information in this email and any attachments is for the sole use of the intended recipient(s). If you are not an intended recipient, or a person responsible for delivering it to an intended recipient, you should delete it from your system immediately without disclosing its contents elsewhere and advise the sender by returning the email or by telephoning a number contained in the body of the email. No responsibility is accepted for loss or damage arising from viruses or changes made to this message after it was sent and the recipient must ensure that the email (and attachments) are virus free. The views contained in this email are those of the author and not necessarily those of Liverpool John Moores University. We will use the personal data information provided by you to respond to your email. For information about how we process personal data and monitor communications please see our Privacy Notice. https://www.ljmu.ac.uk/legal/privacy-and-cookies-- Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus. www.avg.com
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 | paz at acase.org | www.acase.org> On May 19, 2025, at 3:08?AM, Rui Barradas <ruipbarradas at 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 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[[alternative HTML version deleted]]