[R] question about if else
Liaw, Andy
andy_liaw at merck.com
Fri Feb 27 20:05:33 CET 2004
You need to (re-)read ?ifelse. In ifelse(L, v1, v2), L is suppose to be a
vector of logicals (or an expression that evaluates to one), and v1 and v2
are vectors of same length as L; i.e., ifelse() vectorizes if ... else ....
In the first case:
r = ifelse(length(c())!=0, c(), c(1,2))
length(c()) != 0 is FALSE, so you just get 1 as the answer. In the second
case,
r = ifelse(length(c())==0, c(), c(1,2))
length(c()) == 0 is TRUE, so ifelse tries to return the first element of
c(), which does not exist.
I suspect you really want if ... else ....
Andy
> From: Svetlana Eden
>
> Today is a good day for asking question, I guess.
>
> > c()
> NULL
> >
> > length(c())==0
> [1] TRUE
> >
> > r = ifelse(length(c())!=0, c(), c(1,2)) ### OK
> > r = c() ### OK
> > r = ifelse(length(c())==0, c(), c(1,2)) ### why this is
> not OK (given
> > the previous two)?
> Error in "[<-"(`*tmp*`, test, value = rep(yes, length =
> length(ans))[test]) :
> incompatible types
> >
> > c() == NULL
> logical(0)
> >
> > r = ifelse(c()==NULL, c(), c(1,2)) ### why this line does not
> > r ### result in error -
> logical(0) ### 'c()==NULL' is not TRUE
> and not FALSE ?
> >
> >
>
> --
> Svetlana Eden Biostatistician II School of Medicine
> Department of Biostatistics Vanderbilt
> University
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
>
------------------------------------------------------------------------------
Notice: This e-mail message, together with any attachments,...{{dropped}}
More information about the R-help
mailing list