mtcars_Database

Hi everyone,

As a newbie, I am having troubles retrieving a particular detail from this data set. I wanted to see a car brand with the highest "Qsec" stat in the table and expected R to return something like "Merc 230 - 22.90". My line of code was "mtcars %>% max(mtcars$qsec)" and it returned "472" which was not what I wanted. And the first column doesn't have a name. What is the line of code to retrieve what I want? Thank you!

You can use use the base R function which.max() to get the index of the max value and then use this index to extract the corresponding car brand name:

index_max_qsec <- which.max(mtcars$qsec)
car_brand_max_qsec <- mtcars[index_max_qsec, c("vs","qsec","gear")]
car_brand_max_qsec
2 Likes

In addition to the base option shared by @TalhaAsif , here is a tidyverse approach.

library(dplyr)

mtcars %>% filter(qsec == max(qsec)) %>% select(qsec)
#>          qsec
#> Merc 230 22.9
3 Likes

Thank you very much @TalhaAsif

Thank you very much @scottyd22

This topic was automatically closed 42 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.