[R] converting a row of a data.frame to a vector

Bill.Venables at csiro.au Bill.Venables at csiro.au
Wed May 16 02:23:39 CEST 2007

If the data frame has factors and numeric vectors, there is a question
on what form you want the row vector to be in. Only a data frame (list)
can have a mixture of the two. 


> dat <- data.frame(x=1:3, y=4:6, z=letters[1:3])
> (r1 <- dat[1,])
  x y z
1 1 4 a
> class(r1)
[1] "data.frame"
> (r1 <- data.matrix(dat)[1,])
x y z 
1 4 1 
> class(r1)
[1] "integer"
> (r1 <- as.matrix(dat)[1,])
  x   y   z 
"1" "4" "a" 
> class(r1)
[1] "character"

Bill Venables
CSIRO Laboratories
PO Box 120, Cleveland, 4163
Office Phone (email preferred):   +61 7 3826 7251
Fax (if absolutely necessary):      +61 7 3826 7304
Mobile:                                   (I don't have one!)
Home Phone:                            +61 7 3286 7700
mailto:Bill.Venables at csiro.au

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Andrew Yee
Sent: Wednesday, 16 May 2007 9:37 AM
To: r-help at stat.math.ethz.ch
Subject: [R] converting a row of a data.frame to a vector

I've searched for the answer to this in the help list archive, but
able to get the answer to work.

I'm interested in converting a row of a data.frame into a vector.

However, when I use as.vector(x,[1,]) I get another data.frame, instead
of a
vector.  (On the other hand, when I use as.vector(x,[,1]), I get a


	[[alternative HTML version deleted]]

R-help at stat.math.ethz.ch mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list