-2

I need to display name of the car which has max cylinders. Only name nothing else

   #READING THE AUTO FILE
myfile=read.csv("Auto.csv")
#creating the matrix
mpg =c(myfile$mpg)
cylinders=c(myfile$cylinders)
displacement=c(myfile$displacement)
horsepower=c(myfile$horsepower)
weight=c(myfile$weight)
acceleration=c(myfile$acceleration)
year=c(myfile$year)
origin=c(myfile$origin)
name=c(myfile$name)
matrixAuto=matrix(c(mpg,cylinders,displacement,horsepower,weight,acceleration,year,origin),20,8)
matrixAuto
rownames(matrixAuto)=c("chevrolet chevelle malibu","buick skylark 320","plymouth satellite","amc rebel sst","ford torino","ford galaxie 500","chevrolet impala","plymouth fury iii","pontiac catalina","amc ambassador dpl","dodge challenger se","plymouth 'cuda 340","chevrolet monte carlo","buick estate wagon (sw)","toyota corona mark ii","plymouth duster","amc hornet","ford maverick","datsun pl510","volkswagen 1131 deluxe sedan")
matrixAuto
colnames(matrixAuto)=c("mpg","cylinders","displacement","horsepower","weight","accelaration","year","origin")
m=max(cylinders)==cylinders

HOW DO I RETRIEVE THE car NAME USING m?

Also I solved it but didn't understand why this worked rownames(matrixAuto[m,])

Riten Bhagra
  • 1
  • 2
  • 5
  • Did you do any research on your own yet? – JulianG Jul 22 '20 at 10:14
  • @MartinGal Thank you for the suggestion, I am new to community and programming. Will take care of it in the future. – Riten Bhagra Jul 22 '20 at 10:18
  • @JulianG Yes I did. I tried using which.max function which returns the index, but still no use. I wonder why this won't work colnames(matrixAuto[m]). – Riten Bhagra Jul 22 '20 at 10:19
  • I solved it but didn't understand why this worked rownames(matrixAuto[m,]) – Riten Bhagra Jul 22 '20 at 10:29
  • Please take a look at [How to make a great reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and [How to ask](https://stackoverflow.com/help/how-to-ask). Regarding your question: I look at your code and find it rather confusing. It's no reproducible for me. If you break it down into a simple, understandable, reproducible question, you are more likely to get a good answer. – Martin Gal Jul 22 '20 at 10:46
  • 1
    @MartinGal Cheers mate, will take care of it in the future. Thank you for your help, have a good day. – Riten Bhagra Jul 22 '20 at 11:27

1 Answers1

0

Let's take a look at the dataset mtcars which is quite similar to your dataset and it's included in R:

rownames(mtcars[mtcars$cyl==max(mtcars$cyl),])

returns every car name with the maximum amount of cylinders. How does it work?

> mtcars$cyl==max(mtcars$cyl)
[1] FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE
[21] FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE

gives a vector that corresponds to each row of the dataset mtcars. It contains a boolean depending on that given row has the maximum amount of cylinders or not. Therefore

mtcars[mtcars$cyl==max(mtcars$cyl),]

returns you just the rows of your cars with max cylinders.

Since you just want the name of the cars and mtcars is a dataset with named rows, you get them via

> rownames(mtcars[mtcars$cyl==max(mtcars$cyl),])
 [1] "Hornet Sportabout"   "Duster 360"          "Merc 450SE"          "Merc 450SL"          "Merc 450SLC"        
 [6] "Cadillac Fleetwood"  "Lincoln Continental" "Chrysler Imperial"   "Dodge Challenger"    "AMC Javelin"        
[11] "Camaro Z28"          "Pontiac Firebird"    "Ford Pantera L"      "Maserati Bora"  
Martin Gal
  • 16,640
  • 5
  • 21
  • 39
  • i tried the same to find max accelaration using maxAccelaration=max(acceleration)==acceleration rownames(matrixAuto[maxAccelaration,]) but I get null as answer – Riten Bhagra Jul 22 '20 at 11:25
  • Perhaps you have a dataset with `NULL` in acceleration. You should handle that first. – Martin Gal Jul 22 '20 at 11:28
  • `maxAccelaration` gives me `[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [19] FALSE TRUE` as a result but rownames(matrixAuto[maxAccelaration,]) gives me NULL as a result though I have assigned a row name to that field. How is that possible? – Riten Bhagra Jul 22 '20 at 11:46
  • Thanks for your help but still I cannot find the maximum accelaration. – Riten Bhagra Jul 22 '20 at 12:11
  • Take a look at `matrixAuto[maxAccelaration,]`: Does it give you a correct output (with a named row)? – Martin Gal Jul 22 '20 at 12:20
  • it give me the correct output without the row name : `mpg cylinders displacement` `horsepower weight accelaration year origin` `21 6 200 85 2587 16 70 1 ` – Riten Bhagra Jul 22 '20 at 13:27
  • So apparently I changed the .csv file and made changes so that `matrixAuto[maxAccelaration,] ` returned 2 values instead of 1 and my code worked. So, now how do I edit my code to retrieve a row name of a single value? – Riten Bhagra Jul 22 '20 at 13:47