Ordering Dataframe by vector while keeping indices

Let's say I have this dataframe named result :

  id nobs
1 25  463
2 26  586
3 27  338
4 28  475
5 29  711
6 30  932

      

and this vector:

ord = c(30, 29, 28, 27, 26, 25)

      

Now I understand that I can order a dataframe according to the ord vector by doing the following:

result[match(ord, result$id),]

      

to get the following result:

  id nobs
6 30  932
5 29  711
4 28  475
3 27  338
2 26  586
1 25  463

      

However, as you can see, the row indices have also been changed (6,5,4 ..). I want to keep them as before (1,2,3 ...).

How can i do this?

+3


source to share


1 answer


You can simply clear the row names after grabbing a new dataframe:



res <- result[match(ord, result$id),]
row.names(res) <- NULL
res
#   id nobs
# 1 30  932
# 2 29  711
# 3 28  475
# 4 27  338
# 5 26  586
# 6 25  463

      

+1


source







All Articles