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)) > ax 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) > bx 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
Jorge Ivan Velez
2009-Sep-17 02:50 UTC
[R] What is the best way to get a subset of a data.frame?
Hi Peng, Take a look at Juliet Hannah's post at http://www.nabble.com/correct-way-to-subset-a-vector-to24412577.html#a24412577 for some alternatives. <http://www.nabble.com/correct-way-to-subset-a-vector-to24412577.html#a24412577> HTH, Jorge On Wed, Sep 16, 2009 at 10:14 PM, Peng Yu <> wrote:> 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 > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Petr PIKAL
2009-Sep-17 15:49 UTC
[R] Odp: What is the best way to get a subset of a data.frame?
Hi r-help-bounces at r-project.org napsal dne 17.09.2009 04:14:24:> 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) > > bwhat is wrong on b[1:2,] Regards Petr> 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 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.