I have two indicator variables ABS and DEFF. I want to create another indicator variable which will take value 1 if either ABS=1 or DEFF=1. Otherwise, it will take value 0. How can I make that? [[alternative HTML version deleted]]
Try ifelse(ABS ==1 | DEFF == 1, 1, 0) HTH, Jorge.- On Fri, Mar 22, 2013 at 12:02 AM, Tasnuva Tabassum <t.tasnuva@gmail.com>wrote:> I have two indicator variables ABS and DEFF. I want to create another > indicator variable which will take value 1 if either ABS=1 or DEFF=1. > Otherwise, it will take value 0. How can I make that? > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
On 03/21/2013 02:08 PM, Jorge I Velez wrote:> Try > > ifelse(ABS ==1 | DEFF == 1, 1, 0)or as.numeric(ABS | DEFF) (faster?) G?ran> > HTH, > Jorge.- > > > > On Fri, Mar 22, 2013 at 12:02 AM, Tasnuva Tabassum <t.tasnuva at gmail.com>wrote: > >> I have two indicator variables ABS and DEFF. I want to create another >> indicator variable which will take value 1 if either ABS=1 or DEFF=1. >> Otherwise, it will take value 0. How can I make that? >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
I would use a logical variable, with values TRUE and FALSE, instead of a numeric indicator. E.g., I find the following easier to follow bL <- ABS==1 | DEFF==1 if (any(bL)) { do.this() } than bN <- ifelse(ABS == 1 | DEFF == 1, 1, 0) if (any(bN == 1)) { do.this() } The latter leaves me wondering what other values bN might have; the former makes it clear this is a TRUE/FALSE dichotomy. Logicals get converted to numbers (FALSE->0, TRUE->1) when used in arithmetic so you can do, e.g., mean(bL) to see what proportion of your cases satisfy the condition. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of Tasnuva Tabassum > Sent: Thursday, March 21, 2013 6:03 AM > To: R help > Subject: [R] Help on indicator variables > > I have two indicator variables ABS and DEFF. I want to create another > indicator variable which will take value 1 if either ABS=1 or DEFF=1. > Otherwise, it will take value 0. How can I make that? > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
What Bill says, plus the fact that the default handling of logicals in modelling is to convert them to factors and then use treatment contrasts, which will effectively give you the right indicator variables automagically (This in contrast to SAS where you can confuse yourself by declaring a 0/1 variable as a CLASS variable):> cond <- rep(c(TRUE, FALSE),4) > y <- rnorm(8) > lm(y~cond)Call: lm(formula = y ~ cond) Coefficients: (Intercept) condTRUE -0.5741 1.1845> lm(y~as.numeric(cond))Call: lm(formula = y ~ as.numeric(cond)) Coefficients: (Intercept) as.numeric(cond) -0.5741 1.1845 On Mar 21, 2013, at 15:49 , William Dunlap wrote:> I would use a logical variable, with values TRUE and FALSE, instead > of a numeric indicator. E.g., I find the following easier to follow > bL <- ABS==1 | DEFF==1 > if (any(bL)) { do.this() } > than > bN <- ifelse(ABS == 1 | DEFF == 1, 1, 0) > if (any(bN == 1)) { do.this() } > The latter leaves me wondering what other values bN might have; > the former makes it clear this is a TRUE/FALSE dichotomy. > > Logicals get converted to numbers (FALSE->0, TRUE->1) when used in > arithmetic so you can do, e.g., mean(bL) to see what proportion of > your cases satisfy the condition. > > Bill Dunlap > Spotfire, TIBCO Software > wdunlap tibco.com > > >> -----Original Message----- >> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf >> Of Tasnuva Tabassum >> Sent: Thursday, March 21, 2013 6:03 AM >> To: R help >> Subject: [R] Help on indicator variables >> >> I have two indicator variables ABS and DEFF. I want to create another >> indicator variable which will take value 1 if either ABS=1 or DEFF=1. >> Otherwise, it will take value 0. How can I make that? >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Peter Dalgaard, Professor Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
You can define the weights like this ... order.i.want <- c(3, 6, 7, 4, 5, 2, 8, 9, 1, 10) wts <- order(order.i.want) dd.reorder <- reorder(dd, wts, mean) plot(dd.reorder, main="ordered random dendrogram") You should cc R-help on all communication ... that way other readers can follow the thread, and you may get an answer much more quickly to your query. Jean On Fri, Mar 29, 2013 at 11:21 AM, capricy gao <capricyg@yahoo.com> wrote:> Thank you very much for your reply. > > I looked into this and found that this reorder function needs some weight > information, which is very hard to give. In the provided example, > ===========> > set.seed(123) > x <- rnorm(10) > hc <- hclust(dist(x)) > dd <- as.dendrogram(hc) > dd.reorder <- reorder(dd, 10:1) > plot(dd, main = "random dendrogram 'dd'") > > > =========> > if I just want to switch 7 2 4 5 to 7 4 5 2, how can I define the weights ? > > > ------------------------------ > *From:* "Adams, Jean" <jvadams@usgs.gov> > *To:* capricy gao <capricyg@yahoo.com> > *Cc:* R help <r-help@r-project.org> > *Sent:* Monday, March 25, 2013 8:18 AM > *Subject:* Re: [R] Help on dendrogram reorder > > > On Fri, Mar 22, 2013 at 1:48 PM, capricy gao <capricyg@yahoo.com> wrote: > > as.dendrogram > > > ?reorder.dendrogram > > Jean > > >[[alternative HTML version deleted]]