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

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Jan 5 18:59:08 CET 2001


On Fri, 5 Jan 2001, Peter Kleiweg wrote:

> 
> 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.

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

The documentation for as.vector has already been corrected.

Try

> as.vector(F, mode="numeric")
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.

> 
>     > 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 
> 
> -- 
> Peter Kleiweg
> http://www.let.rug.nl/~kleiweg/
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> 

-- 
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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list