order()
In this page:
order()
order(x) returns the indices that would sort x, rather than the sorted values themselves -- this is what lets you sort a whole data frame by one column while keeping other columns aligned. df[order(df$col), ] is the standard idiom for sorting a data frame by a column. Add a minus sign, like order(-df$col), to sort descending.
Note: df[order(df$col), ] is the standard idiom for sorting an entire data frame by one of its columns.
Example: order()
df <- data.frame(name = c("Cara", "Alice", "Bob"), age = c(40, 30, 25))
sorted_df <- df[order(df$age), ]
print(sorted_df)
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: