AW: [R] Getting rows from a dataframe

Tony Plate tplate at blackmesacapital.com
Thu Oct 9 19:11:22 CEST 2003


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:
>I have this right on the desk in front of me. I have gone through most
>of this actually and have been looking for the answer for several
>weeks now before resorting to this. The only reference I've found to
>this is on page 20 under array indexing but didn't see the relation to
>dataframes. Thanks,
>
>Mark
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list