How can I subset a dataframe in a way that the rows in the new subset will have ordered numbers?

Viewed 34

I have a df:

df = data.frame(a = c(1, 2, 3), b = c(3, 2, 1))

I subset the df like so:

df = df[2:3, ]

Now, when I check df as follows:

> df

  a b
2 2 2
3 3 1

the first value has a number 2, and the second number has a number 3. How can I subset the original df in a way that the new subset will have ordered numbers?

Desired output:

> df

  a b
1 2 2
2 3 1
1 Answers

Set the rownames to NULL

`row.names<-`(df[2:3, ], NULL)
  a b
1 2 2
2 3 1

Or change the classtotibbleordata.tablewhich does not allow row.names i.e. it sets the row names toNULL` by default

> tibble::as_tibble(df[2:3, ])
# A tibble: 2 × 2
      a     b
  <dbl> <dbl>
1     2     2
2     3     1
Related