I am facing issue when trying to print the details of cars in decreasing order of mileage. Got this as a class assignment. Thank you for your help
#Homework
I am facing issue when trying to print the details of cars in decreasing order of mileage. Got this as a class assignment. Thank you for your help
#Homework
Can you show what you have tried so far? Paste the code between lines that have three back ticks, like this:
```
Pasted code
```
a<- subset(mtcars, mpg>30)
print(a[order(-a$mpg),])
Above is the code that I am trying to use to get the required output
Are you getting an error message or an unexpected result? It works fine for me.
a<- subset(mtcars, mpg>30)
print(a[order(-a$mpg),])
#> 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
Created on 2022-08-27 with reprex v2.0.2
I am not getting any error message, it's just that not getting expected output. I'll attach the picture of expected output, please check it.
Well, this is basically the same output like @EconProf provided with his code. Lotus Europa
and Honda Civic
have the same mpg
value, therefore it is arbitrary which one should be displayed first. If you really want to have Lotus
before Honda
, just adjust @EconProf's code like so:
a <- subset(mtcars, mpg>30)
print(a[order(-a$mpg,-a$disp),])
#> 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
Created on 2022-08-28 by the reprex package (v2.0.1)
But instead of asking such "basic" questions about filtering etc. in the forum, I think you would be better of to make use of the available introductions to R
in the internet and start learning the basics. Good luck.
Ohh got it thank you so much
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.