[R] What is the best way to get a subset of a data.frame?

Peng Yu pengyu.ut at gmail.com
Thu Sep 17 04:14:24 CEST 2009


Hi,

I want to construct a data.frame 'y' by using x$x and x$y. I think
that there might be better ways to do it (because, for example, we can
use a_matrix[3:5,] to extract certain rows, where 'a_matrix' is a
matrix). Can somebody let know what the best way is?

> a=data.frame(x=1:10,y=rep("abc",10),z=rep("xyz",10))
> a
    x   y   z
1   1 abc xyz
2   2 abc xyz
3   3 abc xyz
4   4 abc xyz
5   5 abc xyz
6   6 abc xyz
7   7 abc xyz
8   8 abc xyz
9   9 abc xyz
10 10 abc xyz
> b=data.frame(x=a$x, y=a$y)
> b
    x   y
1   1 abc
2   2 abc
3   3 abc
4   4 abc
5   5 abc
6   6 abc
7   7 abc
8   8 abc
9   9 abc
10 10 abc



Regards,
Peng




More information about the R-help mailing list