I am using R. I have a data frame with "year , price, mileage" columns. I want to group the df by year first and then sort each group by mileage. How can I do this?
$\begingroup$
$\endgroup$
2
-
$\begingroup$ You should consider asking this sort of question over on StackOverflow, not on Data Science. Additionally, you should share what you have already tried in solving this problem. Where exactly did you go wrong? $\endgroup$– n1k31t4Commented Sep 28, 2018 at 0:54
-
$\begingroup$ Order by year, mileage? $\endgroup$– user2974951Commented Sep 28, 2018 at 6:29
Add a comment
|
1 Answer
$\begingroup$
$\endgroup$
The 'dplyr' package in R is ideal for these types of data manipulation tasks. The arrange function for example can group a dataframe by a certain column, and then sort by another column. For example:
arrange(df, desc(mileage), group_by = year)
See arrange for documentation on the arrange function, and dplyr for the dplyr package description.