I find it slightly surprising, that ifelse(TRUE, character(0), "") returns NA instead of character(0). [WNT 2.6.2 Patched]
Heikki Kaskelma
5 messages · Heikki Kaskelma, Marc Schwartz, Brian Ripley +2 more
I find it slightly surprising, that ifelse(TRUE, character(0), "") returns NA instead of character(0). [WNT 2.6.2 Patched]
Heikki Kaskelma
I find it slightly surprising, that ifelse(TRUE, character(0), "") returns NA instead of character(0). [WNT 2.6.2 Patched]
Time to upgrade... :-)
The same behavior is in:
R version 2.7.2 beta (2008-08-16 r46368)
The reason for this is that the internal code for ifelse() has:
if (any(test[!nas]))
ans[test & !nas] <- rep(yes, length.out = length(ans))[test &
!nas]
where the key code is:
rep(yes, length.out = length(ans))
In this case, 'yes' is character(0) and 'ans' is TRUE.
Thus, you get:
> rep(character(0), length.out = length(TRUE))
[1] NA
This behavior is documented in ?rep, where in the Details section you
will find:
"If x has length zero and length.out is supplied and is positive, the
values are filled in using the extraction rules, that is by an NA of the
appropriate class for an atomic vector (0 for raw vectors) and NULL for
a list. "
Thus:
class(rep(character(0), length.out = length(TRUE)))
[1] "character" which shows that the NA that is returned is of class character, which is the class for character(0). HTH, Marc Schwartz
Another way of looking at this is that ifelse(TRUE, character(0), "") asks for a length-one result containing the first element of yes=character(0), and character(0)[1] is NA_character_ I suspect if(TRUE) character(0) else "" was intended.
on 08/18/2008 06:35 PM Heikki Kaskelma wrote:
I find it slightly surprising, that ifelse(TRUE, character(0), "") returns NA instead of character(0). [WNT 2.6.2 Patched]
Time to upgrade... :-)
The same behavior is in:
R version 2.7.2 beta (2008-08-16 r46368)
The reason for this is that the internal code for ifelse() has:
if (any(test[!nas]))
ans[test & !nas] <- rep(yes, length.out = length(ans))[test &
!nas]
where the key code is:
rep(yes, length.out = length(ans))
In this case, 'yes' is character(0) and 'ans' is TRUE.
Thus, you get:
> rep(character(0), length.out = length(TRUE))
[1] NA This behavior is documented in ?rep, where in the Details section you will find: "If x has length zero and length.out is supplied and is positive, the values are filled in using the extraction rules, that is by an NA of the appropriate class for an atomic vector (0 for raw vectors) and NULL for a list. " Thus:
class(rep(character(0), length.out = length(TRUE)))
[1] "character" which shows that the NA that is returned is of class character, which is the class for character(0). HTH, Marc Schwartz
______________________________________________ R-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Another way of looking at this is that ifelse(TRUE, character(0), "") asks for a length-one result
Just to rub it in: The length of an ifelse() computation is the length of the "test" argument, so there is no way you should expect character(0) as the result. (The class of the result is also taken from "test", which is arguably one of the lesser convenient features in R.)
containing the first element of yes=character(0), and character(0)[1] is NA_character_ I suspect if(TRUE) character(0) else "" was intended. On Mon, 18 Aug 2008, Marc Schwartz wrote:
on 08/18/2008 06:35 PM Heikki Kaskelma wrote:
I find it slightly surprising, that ifelse(TRUE, character(0), "") returns NA instead of character(0). [WNT 2.6.2 Patched]
Time to upgrade... :-)
The same behavior is in:
R version 2.7.2 beta (2008-08-16 r46368)
The reason for this is that the internal code for ifelse() has:
if (any(test[!nas]))
ans[test & !nas] <- rep(yes, length.out = length(ans))[test &
!nas]
where the key code is:
rep(yes, length.out = length(ans))
In this case, 'yes' is character(0) and 'ans' is TRUE.
Thus, you get:
> rep(character(0), length.out = length(TRUE))
[1] NA This behavior is documented in ?rep, where in the Details section you will find: "If x has length zero and length.out is supplied and is positive, the values are filled in using the extraction rules, that is by an NA of the appropriate class for an atomic vector (0 for raw vectors) and NULL for a list. " Thus:
class(rep(character(0), length.out = length(TRUE)))
[1] "character" which shows that the NA that is returned is of class character, which is the class for character(0).
O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Others have commented on why this holds. There is an alternative, 'ifelse1', part of the splus2R package, that does what you'd like here. Tim Hesterberg
I find it slightly surprising, that ifelse(TRUE, character(0), "") returns NA instead of character(0). -- Heikki Kaskelma