Weiwei Shi
2009-Jul-13 03:55 UTC
[R] how to keep row name if there is only one row selected from a data frame
Hi, there: Assume I have a dataframe with rownames like A with rownames like a to e,> A[,1] [,2] a 1 6 b 2 7 c 3 8 d 4 9 e 5 10 when I use A[1,], I lost the rowname for it, like below. How could I keep it? Is there an easy way instead that I have to modify by myself after I used A[1,] manually.> A[1,][1] 1 6 Thanks, W. -- Weiwei Shi, Ph.D Research Scientist GeneGO, Inc. "Did you always know?" "No, I did not. But I believed..." ---Matrix III [[alternative HTML version deleted]]
Moshe Olshansky
2009-Jul-13 05:02 UTC
[R] how to keep row name if there is only one row selected from a data frame
Try A[1,,drop=FALSE] - see help("\[") --- On Mon, 13/7/09, Weiwei Shi <helprhelp at gmail.com> wrote:> From: Weiwei Shi <helprhelp at gmail.com> > Subject: [R] how to keep row name if there is only one row selected from a data frame > To: "r-help at stat.math.ethz.ch" <R-help at stat.math.ethz.ch> > Received: Monday, 13 July, 2009, 1:55 PM > Hi, there: > > Assume I have a dataframe with rownames like A with > rownames like a to e, > > > A > ? [,1] [,2] > a? ? 1? ? 6 > b? ? 2? ? 7 > c? ? 3? ? 8 > d? ? 4? ? 9 > e? ? 5???10 > > when I use A[1,], I lost the rowname for it, like below. > How could I keep > it? Is there an easy way instead that I have to modify by > myself after I > used A[1,] manually. > > > A[1,] > [1] 1 6 > > Thanks, > > W. > > > > -- > Weiwei Shi, Ph.D > Research Scientist > GeneGO, Inc. > > "Did you always know?" > "No, I did not. But I believed..." > ---Matrix III > > ??? [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >
hadley wickham
2009-Jul-13 05:53 UTC
[R] how to keep row name if there is only one row selected from a data frame
On Mon, Jul 13, 2009 at 5:55 AM, Weiwei Shi<helprhelp at gmail.com> wrote:> Hi, there: > > Assume I have a dataframe with rownames like A with rownames like a to e, > >> A > ?[,1] [,2] > a ? ?1 ? ?6 > b ? ?2 ? ?7 > c ? ?3 ? ?8 > d ? ?4 ? ?9 > e ? ?5 ? 10 > > when I use A[1,], I lost the rowname for it, like below. How could I keep > it? Is there an easy way instead that I have to modify by myself after I > used A[1,] manually. > >> A[1,] > [1] 1 6I think if you do str(A) you'll discover it's a matrix, not a data frame. Hadley -- http://had.co.nz/