[R] need help in if else condition

peter dalgaard pd@|gd @end|ng |rom gm@||@com
Sun Jul 14 22:11:14 CEST 2019


One common gotcha is that ifelse() has no way to reconcile attributes between the two alternatives so it takes the attributes for the result from those of the condition, which is almost certainly what you don't want. In particular, this may convert factors to their underlying integer codes. 

Another gotcha is that both alternatives are (usually) computed for all indices, whether or not that is sensible. (See, e.g., first example on help page.)

-pd

> On 10 Jul 2019, at 15:33 , José María Mateos <chema using rinzewind.org> wrote:
> 
> On Wed, Jul 10, 2019, at 04:39, Eric Berger wrote:
>> 1. The ifelse() command is a bit tricky in R. Avoiding it is often a good
>> policy.
> 
> You piqued my curiosity, can you elaborate a bit more on this?
> 
> -- 
> José María (Chema) Mateos || https://rinzewind.org
> 
> ______________________________________________
> R-help using 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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes using cbs.dk  Priv: PDalgd using gmail.com



More information about the R-help mailing list