naw3 at duke.edu
2008-Jun-24 15:23 UTC
[R] reset row numbers when extracting a subset of a table
Hi I created a new table by extracting only certain rows of table, but the row numbers in my new table correspond to those from the original table. Is there a way to reset the row numbers in my new table so that they start from one? like below: my table: COL1 COL2 17 v 45 18 b 14 25 x 98 desired: COL1 COL2 1 v 45 2 b 14 3 x 98 Thank you! -Nina
Gabor Csardi
2008-Jun-24 15:55 UTC
[R] reset row numbers when extracting a subset of a table
Nina, these are not row NUMBERS, but row NAMES. Numbers are actually reset, they always start with 1 and they are continuous. Just try doing T[1,] on your table. If you want to reset row names, you can do this: rownames(T) <- seq(length=nrow(T)) or you can even remove them: rownames(T) <- NULL Best, Gabor On Tue, Jun 24, 2008 at 11:23:56AM -0400, naw3 at duke.edu wrote:> Hi > > I created a new table by extracting only certain rows of table, but the row > numbers in my new table correspond to those from the original table. Is there a > way to reset the row numbers in my new table so that they start from one? like > below: > > my table: > COL1 COL2 > 17 v 45 > 18 b 14 > 25 x 98 > > desired: > COL1 COL2 > 1 v 45 > 2 b 14 > 3 x 98 > > Thank you! > -Nina > > ______________________________________________ > 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.-- Csardi Gabor <csardi at rmki.kfki.hu> UNIL DGM
Gabor Grothendieck
2008-Jun-24 16:22 UTC
[R] reset row numbers when extracting a subset of a table
I assume that by table you mean data frame. Using the built in data frame, BOD, try this:> ix <- 2:4 > BOD[ix, ]Time demand 2 2 10.3 3 3 19.0 4 4 16.0> data.frame(BOD[ix, ], row.names = seq_along(ix))Time demand 1 2 10.3 2 3 19.0 3 4 16.0 On Tue, Jun 24, 2008 at 11:23 AM, <naw3 at duke.edu> wrote:> Hi > > I created a new table by extracting only certain rows of table, but the row > numbers in my new table correspond to those from the original table. Is there a > way to reset the row numbers in my new table so that they start from one? like > below: > > my table: > COL1 COL2 > 17 v 45 > 18 b 14 > 25 x 98 > > desired: > COL1 COL2 > 1 v 45 > 2 b 14 > 3 x 98 > > Thank you! > -Nina > > ______________________________________________ > 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. >