Skip to content

Why doesn't as.vector() return a vector?

2 messages · Peter Kleiweg, Brian Ripley

#
I use as.vector() on a data frame and I get a data frame in
return. No warning. I have to use as.matrix() first. Why is
that? Doesn't make sense to me. I'm using R 1.2.0 on Linux.

    > F <- data.frame(a = c(1,2,3), b = c(4,5,6))
    > F
      a b
    1 1 4
    2 2 5
    3 3 6
    > V <- as.vector(F)
    > V
      a b
    1 1 4
    2 2 5
    3 3 6
    > attributes(V)
    $names
    [1] "a" "b"
    
    $row.names
    [1] "1" "2" "3"
    
    $class
    [1] "data.frame"
    
    > as.vector(as.matrix(F))
    [1] 1 2 3 4 5 6
#
On Fri, 5 Jan 2001, Peter Kleiweg wrote:

            
A data frame is a list. A list is a vector, ....

The documentation for as.vector has already been corrected.

Try
Error in as.vector(F, mode = "numeric") : (list) object cannot be coerced
to vector type 14

which is probably what you might have expected, and correctly gives an
error.