0

When I order a data frame by the values of a column using the function "order", I get the rows reordered but they preserve rownames, which they are numbers if I am not using rownames in my data frame.

Example:

mydf <- data.frame(first_name=c("Arya", "Cersei", "Daeneris"), age= c(11, 32, 19))
mydf

mydf <- mydf[order(mydf$age, decreasing=T),]
mydf

Edit: This produces a data frame with the row names preserving the previous row order:

  first_name age
2     Cersei  32
3   Daeneris  19
1       Arya  11

I have looked into "order" help, but I don't find any argument to drop the row names. Is there any way to do that even using a different function to reorder rows?

I know I can use rownames(mydf)=NULL but I would like to do it in the order function if possible.

Edit: desired output would be:

  first_name age
1     Cersei  32
2   Daeneris  19
3       Arya  11
TLaguna
  • 21
  • 3
  • 1
    I am not entirely sure what your desired output is because your description is somewhat vague. Would you mind providing an example of the desired output? Otherwise it is hard to help you. If you want to re-assign the row.names, just use `row.names(mydf) <- 1:nrow(mydf)`. – freyberg May 09 '23 at 12:28
  • I know I can do that (as I wrote in the question) `rownames(mydf)<-NULL`, but I am interested in doing it within the "order" function, if possible, i.e. using a specific argument. – TLaguna May 16 '23 at 13:14

0 Answers0