[R] ifelse problem
Uwe Ligges
ligges at statistik.uni-dortmund.de
Fri Mar 10 15:31:37 CET 2006
Adrian DUSA wrote:
> Dear all,
>
> There is something I'm missing in order to understand the following behavior:
>
>
>>aa <- c("test", "name")
>>ifelse(any(nchar(aa) < 3), aa[-which(nchar(aa) < 3)], aa)
> [1] "test"
>
>>any(nchar(aa) < 3)
>
> [1] FALSE
>
> Shouldn't the ifelse function return the whole aa vector?
No, ifelse() works vetorized, see ?ifelse. You do not want such a behaviour.
In fact, you do not want "if" in any combination with "else", but:
aa[!(nchar(aa) < 3)]
Uwe Ligges
> Using if and else separately, I get the correct result...
>
>
>>if (any(nchar(aa) < 3)) {
>
> aa[-which(nchar(aa) < 3)]
> } else {
> aa
> }
> [1] "test" "name"
>
>
> Thanks in advance,
> Adrian
>
More information about the R-help
mailing list