[R] data.frame into vector
Prof Brian Ripley
ripley at stats.ox.ac.uk
Tue Nov 23 18:14:01 CET 2004
A data frame is a list, and a list is a vector. Once you understand that,
yoy may understand what you are seeing.
as.matrix(x)[1,] seems to be one of the easiest ways to get what you want
On Tue, 23 Nov 2004, Tiago R Magalhaes wrote:
> Hi
>
> I want to extract a row from a data.frame but I want that object to be a
> vector . After trying some different ways I end up always with a data.frame
> or with the wrong vector. Any pointers?
>
> x <- data.frame(a = factor(c('a',2,'b')), b = c(4,5,6))
> I want to get
> "a" "4"
>
> I tried:
>
> as.vector(x[1,])
> a b
> 1 a 4
> (resulting in a data.frame even after in my mind having coerced it into a
> vector!)
>
> as.vector(c[1,], numeric='character')
Eh? I get an error there.
> [1] "2" "4"
> (almost what I want, except that "2" instead of "a" - I guess this as to do
> with levels and factors)
--
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 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list