We were merely trying to re-order the rows and the result was converted to a vector. This happened because the rules for
[ , ]change if there is only one result column. This happens even if the there had been only one input column. Another example is:
d[,]is also vector in this case.
The issue is: if we are writing re-usable code we are often programming before we know complete contents of a variable or argument. For a
g” supplied as an argument:
g[vec, ]can be a
vector(or even possibly a
list). However we do know if
g[vec, , drop = FALSE]is also a
vecis a vector of valid row indices or a
NAinduces some special cases).
We care as
data.frames have different semantics, so are not fully substitutable in later code.
Definitely read the comments on this one as well, as John extends his explanation and others chime in with very useful notes.