I want to return a data.frame from a function if TRUE, else return NA using return(ifelse(condition, mydf, NA))
However, ifelse strips the column names from the data.frame.
Why are these results different?
> data.frame(1)
X1
1 1
> ifelse(TRUE, data.frame(1), NA)
[[1]]
[1] 1
Some additional insight from dput():
> dput(ifelse(TRUE, data.frame(1), 0))
list(1)
> dput(data.frame(1))
structure(list(X1 = 1), .Names = "X1", row.names = c(NA, -1L),
class = "data.frame")
ifelse
is generally intended for vectorized comparisons, and has side-effects such as these: as it says in?ifelse
,so in this case (
test
is a vector of length 1) it tries to convert the data frame to a 'vector' (list in this case) of length 1 ...As a general design point I try to return objects of the same structure no matter what, so I might prefer
As a general rule, I find that R users (especially coming from other programming languages) start by using
if
exclusively, take a while to discoverifelse
, then overuse it for a while, discovering later that you really want to useif
in logical contexts. A similar thing happens with&
and&&
.See also: