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() == NULLlogical(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
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}}
ifelse() has three arguments, named 'test', 'yes', and 'no'. In both of your two examples, you gave it a test argument of length equal to 1. That is, both length(c())!=0 and length(c())==0 are expressions which when evaluated have length equal to 1. Therefore, the ifelse() function wants to return an object of length 1. So, it wants to return the first element of either the 'yes' argument, or the 'no' argument, depending on whether test is true or false. But c() has length zero, there is no first element available to return. So you get an error message. -Don At 12:12 PM -0600 2/27/04, Svetlana Eden wrote:>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-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA