Henrik Bengtsson
2021-Dec-01 17:59 UTC
[Rd] R-devel: as.vector(x, mode = "list") drops attributes despite documented not to
Hi, in R 4.1.2 we have:> x <- structure(as.list(1:2), dim = c(1,2)) > x[,1] [,2] [1,] 1 2> as.vector(x, mode = "list")[,1] [,2] [1,] 1 2 whereas in recent versions of R-devel (4.2.0) we have:> x <- structure(as.list(1:2), dim = c(1,2)) > x[,1] [,2] [1,] 1 2> as.vector(x, mode = "list")[[1]] [1] 1 [[2]] [1] 2 However, as I read ?as.vector, dropping of attributes should _not_ happen for non-atomic results such as lists. Is the new behavior a mistake? Specifically, ?as.vector says: 'as.vector', a generic, attempts to coerce its argument into a vector of mode 'mode' (the default is to coerce to whichever vector mode is most convenient): if the result is atomic all attributes are removed. [...] Details: The atomic modes are "logical", "integer", "numeric" (synonym "double"), "complex", "character" and "raw". [...] On the other hand, as.vector removes all attributes including names for results of atomic mode (but not those of mode "list" nor "expression"). Value: [...] For as.vector, a vector (atomic or of type list or expression). All attributes are removed from the result if it is of an atomic mode, but not in general for a list result. The default method handles 24 input types and 12 values of type: the details of most coercions are undocumented and subject to change. /Henrik