Skip to content
Prev 38503 / 398500 Next

AW: [R] Getting rows from a dataframe

If you're having so much trouble, perhaps it's because you want to get a 
vector result?  This requires a little more, and if so, perhaps one of the 
following provides what you are looking for:

 > x <- data.frame(a=1:3,b=4:6)
 > # row as a data frame
 > x[2,]
   a b
2 2 5
 > # row as a list
 > x[2,,drop=T]
$a
[1] 2

$b
[1] 5

 > # row as a vector
 > unlist(x[2,,drop=T])
a b
2 5
 > # row as a vector again
 > unlist(x[2,])
a b
2 5
 > # row as a matrix (if x contains any non-numeric columns, this will be a 
character matrix)
 > as.matrix(x[2,])
   a b
2 2 5
 > # row as a vector (if x contains any non-numeric columns, this will be a 
character vector)
 > as.matrix(x)[2,]
a b
2 5
 > # row as a numeric vector (non-numeric columns in x will be converted to 
numeric data, see ?data.matrix for how)
 > data.matrix(x[2,])
   a b
2 2 5
 >

Tony Plate
At Thursday 05:40 PM 10/9/2003 +0100, Mark Lee wrote: