0

This is my dataframe

                 mpg cyl disp  hp drat    wt  qsec vs am gear carb
Fiat 128       32.4   4 78.7  66 4.08 2.200 19.47  1  1    4    1
Honda Civic    30.4   4 75.7  52 4.93 1.615 18.52  1  1    4    2
Toyota Corolla 33.9   4 71.1  65 4.22 1.835 19.90  1  1    4    1
Lotus Europa   30.4   4 95.1 113 3.77 1.513 16.90  1  1    5    2
mtcars[which(mtcars$mpg > 30), ]

I have used the above code to get the R dataframe, But how can I sort it such that it is shown in decreasing order of mileage? And i would prefer not using any libraries like dplyr

I have to get the output whereby Lotus Europa comes first then Honda Civic (although they have same mileage)

jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • Ok three points: 1) This seems like a hw question. You should add a self-study tab. 2) Your original request was to sort by mpg which is shown in the solution I provided. 3) You've now added a second request (no dplyr and now Lotus Europa needs to come first). Just add a second sort on the disp variable in descending mode to have that outcome. – user1357015 Jan 17 '22 at 03:46
  • @user1357015 so do you mean modify the code for the second code? mtcars[which(mtcars$mpg>30),][order(mtcars[which(mtcars$mpg>30),"mpg"], decreasing = TRUE),] – fyceheist Jan 17 '22 at 03:50
  • yes. or use dplyr. or modify example 3 that I showed. – user1357015 Jan 17 '22 at 03:51
  • @user1357015 do we add a new [] or edit the second []? – fyceheist Jan 17 '22 at 03:54
  • Does this answer your question? [Sort (order) data frame rows by multiple columns](https://stackoverflow.com/questions/1296646/sort-order-data-frame-rows-by-multiple-columns) – user1357015 Jan 17 '22 at 03:55
  • @user1357015 like this? mtcars[which(mtcars$mpg>30),][order(mtcars[which(mtcars$mpg>30),"mpg"], decreasing = TRUE),][order(mtcars[which(mpg"], decreasing = FALSE),] – fyceheist Jan 17 '22 at 03:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/241121/discussion-between-fyceheist-and-user1357015). – fyceheist Jan 17 '22 at 03:57
  • @user1357015 or is it possible to edit/add your answer? – fyceheist Jan 17 '22 at 04:09

2 Answers2

0

I recommend using dplyr for this which is a very common package.

library(dplyr)
mtcars[which(mtcars$mpg>30),] %>% arrange(desc(mpg), descending = FALSE)

               mpg cyl disp  hp drat    wt  qsec vs am gear carb
Toyota Corolla 33.9   4 71.1  65 4.22 1.835 19.90  1  1    4    1
Fiat 128       32.4   4 78.7  66 4.08 2.200 19.47  1  1    4    1
Honda Civic    30.4   4 75.7  52 4.93 1.615 18.52  1  1    4    2
Lotus Europa   30.4   4 95.1 113 3.77 1.513 16.90  1  1    5    2

In fact, dplyr has a pipe operator which makes the filtering easier too.

mtcars %>% 
  filter(mpg > 30)%>% 
  arrange(desc(mpg), descending = FALSE)
               
                mpg cyl disp  hp drat    wt  qsec vs am gear carb
Toyota Corolla 33.9   4 71.1  65 4.22 1.835 19.90  1  1    4    1
Fiat 128       32.4   4 78.7  66 4.08 2.200 19.47  1  1    4    1
Honda Civic    30.4   4 75.7  52 4.93 1.615 18.52  1  1    4    2
Lotus Europa   30.4   4 95.1 113 3.77 1.513 16.90  1  1    5    2

If you must use base R, it's (in my opinion) not as syntactically pretty but just as valid:

x <- mtcars[which(mtcars$mpg>30),] #this makes a new dataframe which is easier to work with
x[order(x$mpg, decreasing = TRUE),]

                mpg cyl disp  hp drat    wt  qsec vs am gear carb
Toyota Corolla 33.9   4 71.1  65 4.22 1.835 19.90  1  1    4    1
Fiat 128       32.4   4 78.7  66 4.08 2.200 19.47  1  1    4    1
Honda Civic    30.4   4 75.7  52 4.93 1.615 18.52  1  1    4    2
Lotus Europa   30.4   4 95.1 113 3.77 1.513 16.90  1  1    5    2

Finally, if you insist on one line, you can even do that.

mtcars[which(mtcars$mpg>30),][order(mtcars[which(mtcars$mpg>30),"mpg"], decreasing = TRUE),]
                mpg cyl disp  hp drat    wt  qsec vs am gear carb
Toyota Corolla 33.9   4 71.1  65 4.22 1.835 19.90  1  1    4    1
Fiat 128       32.4   4 78.7  66 4.08 2.200 19.47  1  1    4    1
Honda Civic    30.4   4 75.7  52 4.93 1.615 18.52  1  1    4    2
Lotus Europa   30.4   4 95.1 113 3.77 1.513 16.90  1  1    5    2
user1357015
  • 11,168
  • 22
  • 66
  • 111
0

order it first, then subset.

Using decreasing=TRUE, orderes both the mileage and the row names in the desired order. The which is actually superfluous.

mtcars[order(mtcars$mpg, rownames(mtcars), decreasing=TRUE), ] |> 
  subset(mpg > 30)
#                 mpg cyl disp  hp drat    wt  qsec vs am gear carb
# Toyota Corolla 33.9   4 71.1  65 4.22 1.835 19.90  1  1    4    1
# Fiat 128       32.4   4 78.7  66 4.08 2.200 19.47  1  1    4    1
# Lotus Europa   30.4   4 95.1 113 3.77 1.513 16.90  1  1    5    2
# Honda Civic    30.4   4 75.7  52 4.93 1.615 18.52  1  1    4    2

Note: R >= 4.1 used.

jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • it says Error: unexpected '>' in: " empdetails <- data.frame(name,age,department) mtcars[order(mtcars$mpg, rownames(mtcars), decreasing=TRUE), ] |>" Execution halted – helloitsme Jan 17 '22 at 15:05
  • @helloitsme Update your R, or do `subset(mtcars[order(mtcars$mpg, rownames(mtcars), decreasing=TRUE), ], mpg > 30)`. – jay.sf Jan 17 '22 at 15:40