Olu Ola
2016-Aug-17 18:24 UTC
[R] Creating dummy variable using ifelse statement while you also retain NA's
Hello,I am trying to create a dummy variable using the ifelse statement. However, the ifelse statement does not recognize na.rm = True. How can I create a dummy variable so that it still retains the missing data denoted as "NA" ? Regards [[alternative HTML version deleted]]
Jeff Newmiller
2016-Aug-17 18:50 UTC
[R] Creating dummy variable using ifelse statement while you also retain NA's
I cannot imagine why you would want ifelse to support an na.rm argument, and your phrase 'still retains the missing data denoted as "NA"' seems exactly how ifelse works anyway. You may need to study how NA values work... basic things like TRUE & NA ==NA and when you should use is.na(). The"Introduction to R" document may be helpful, as might "The R Inferno". The usual boilerplate at this point applies... before posting again, read the Posting Guide, post your question using plain text format, and provide a reproducible example that illustrates your concern on a concrete manner. -- Sent from my phone. Please excuse my brevity. On August 17, 2016 11:24:45 AM PDT, Olu Ola via R-help <r-help at r-project.org> wrote:>Hello,I am trying to create a dummy variable using the ifelse >statement. However, the ifelse statement does not recognize na.rm >True. >How can I create a dummy variable so that it still retains the missing >data denoted as "NA" ? >Regards > [[alternative HTML version deleted]] > >______________________________________________ >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 >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.
William Dunlap
2016-Aug-17 19:25 UTC
[R] Creating dummy variable using ifelse statement while you also retain NA's
You can use nested ifelse() calls, as in x <- c("a", "b", NA, "678") ifelse(is.na(x), NA_integer_, ifelse(grepl("[a-z]", x), 1L, 0L)) #[1] 1 1 NA 0 Note that most modelling functions that need dummy variables use the model.matrix function internally so character/factor data gets converted to dummy variables automatically. Using ifelse to make dummy variables is usually the hard way to do it. Bill Dunlap TIBCO Software wdunlap tibco.com On Wed, Aug 17, 2016 at 11:24 AM, Olu Ola via R-help <r-help at r-project.org> wrote:> Hello,I am trying to create a dummy variable using the ifelse statement. > However, the ifelse statement does not recognize na.rm = True. > How can I create a dummy variable so that it still retains the missing > data denoted as "NA" ? > Regards > [[alternative HTML version deleted]] > > ______________________________________________ > 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 http://www.R-project.org/ > posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]