[R] Help on indicator variables

William Dunlap wdunlap at tibco.com
Thu Mar 21 15:49:47 CET 2013


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.



More information about the R-help mailing list