0

I need to display name of the car which has max acceleration

# 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)

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")

#calculating max cylinders
maxCylinders=max(cylinders)==cylinders
rownames(matrixAuto[maxCylinders,])

But then I tried the same thing for calculating maximum acceleration and only a single value was returned using max function and when I tried using

maxAccelaration=max(acceleration)==acceleration
rownames(matrixAuto[maxAccelaration,])

NULL value was returned. Can you explain me why do I get correct answers for mutliple max values whereas null value for a single max value using my code? Also How do i get the row name of the max acceleration? I am trying to get row name for max acceleration car which is the volkswagen 1131 deluxe sedan

Any help is appreciated.

my file using the dput() function is

structure(list(mpg = c(18L, 15L, 18L, 16L, 17L, 15L, 14L, 14L, 
14L, 15L, 15L, 14L, 15L, 14L, 24L, 22L, 18L, 21L, 27L, 26L), 
    cylinders = c(8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
    8L, 8L, 8L, 4L, 6L, 6L, 6L, 4L, 4L), displacement = c(307L, 
    350L, 318L, 304L, 302L, 429L, 454L, 440L, 455L, 390L, 383L, 
    340L, 400L, 455L, 113L, 198L, 199L, 200L, 97L, 97L), horsepower = c(130L, 
    165L, 150L, 150L, 140L, 198L, 220L, 215L, 225L, 190L, 170L, 
    160L, 150L, 225L, 95L, 95L, 97L, 85L, 88L, 46L), weight = c(3504L, 
    3693L, 3436L, 3433L, 3449L, 4341L, 4354L, 4312L, 4425L, 3850L, 
    3563L, 3609L, 3761L, 3086L, 2372L, 2833L, 2774L, 2587L, 2130L, 
    1835L), acceleration = c(12, 11.5, 11, 12, 10.5, 10, 9, 70.5, 
    10, 8.5, 10, 8, 9.5, 10, 15, 15.5, 15.5, 16, 14.5, 70.5), 
    year = c(70L, 70L, 70L, 70L, 70L, 70L, 70L, 70L, 70L, 70L, 
    70L, 70L, 70L, 70L, 70L, 70L, 70L, 70L, 70L, 70L), origin = c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 
    1L, 1L, 3L, 2L), name = 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")), row.names = c(NA, 20L), class = "data.frame")
  • Please post the `dput` of your *myfile* object for [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5965451). Remember we cannot access `read.csv("Auto.csv")` on our end. Also show us desired result. – Parfait Jul 22 '20 at 14:41
  • @Parfait I have attached an image if it helps you. – Riten Bhagra Jul 22 '20 at 14:57
  • Please, please, no images! R's very useful `dput` (ascii version of data) allows us to copy and run on our end. See: [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question/285557#285557). Help us help you! Simply post result of a few row sample: `dput(head(myfile, 10))` – Parfait Jul 22 '20 at 14:58
  • @Parfait Sorry mate, Posting it – Riten Bhagra Jul 22 '20 at 15:03
  • @Parfait used the dput() function so that you replicate it on your end. Thanks again for your help mate. Appreciate it! – Riten Bhagra Jul 22 '20 at 15:16
  • @Parfait Again sorry for the trouble, will take of it in the future. – Riten Bhagra Jul 22 '20 at 15:30

2 Answers2

0

When subsetting a matrix (or dataframe) with only 1 row, it will return a named vector (no rownames). When you subset more than 1 row, you will get a matrix (or dataframe) back.

Note, that is you use tibble::tibble and subset one row, it will return a tibble.

head(mtcars)

matrixMTCARS <- as.matrix(mtcars)

test <- matrixMTCARS[1, ] 
#subsetting a single row will return just a names vector, no row names in a vector

test2 <- matrixMTCARS[1:2, ]
#subsetting multiple rows will return a matrix, will bring the row names with it

str(matrixMTCARS[1, ])

dim(matrixMTCARS[1, ])
#null since it is a vector, there is no dimensions

str(matrixMTCARS[1:2,])

dim(matrixMTCARS[1:2, ])
MattO
  • 171
  • 1
  • 6
  • `tibble` is the r package and `tibble` is the function. The syntax of `tibble::tibble` explains the package::function. A `tibble` is basically an enhanced dataframe used pretty extensively in the tidyverse. – MattO Jul 22 '20 at 15:30
  • So how do I get the name for the vector? Thanks for the help mate! – Riten Bhagra Jul 22 '20 at 15:33
  • A named vector just means that each entry has a name. Once you subset only one column, you lose the row names. If you dont need to use a matrx for future calculations, I would recommend looking into using dataframe or tibble so you can have mixed columns (characters and numberics, etc.) – MattO Jul 22 '20 at 15:33
0

Consider calculating max values of all numeric columns then filter accordingly. See even easier way to cast data frame to matrix:

Matrix Build

num_cols <- c("mpg", "cylinders", "displacement", "horsepower",
              "weight", "acceleration", "year", "origin")

matrixAuto <- as.matrix(myfile[,num_cols]), 
dimnames(matrixAuto) <- list(myfile$name, num_cols))

# ALTERNATIVELY:
# matrixAuto <- `dimnames<-`(as.matrix(myfile[,num_cols]), list(myfile$name, num_cols))

Matrix Aggregation

max_vals_matrix <- apply(matrixAuto, 2, max)
max_vals_matrix
#     mpg    cylinders displacement   horsepower    weight acceleration   year   origin 
#    27.0          8.0        455.0        225.0    4425.0         70.5   70.0      3.0 

Max Cylinders

mask <- matrixAuto[,"cylinders"] == max_vals_matrix["cylinders"]
matrixAuto[mask,]
#                           mpg cylinders displacement horsepower weight acceleration year origin
# chevrolet chevelle malibu  18         8          307        130   3504         12.0   70      1
# buick skylark 320          15         8          350        165   3693         11.5   70      1
# plymouth satellite         18         8          318        150   3436         11.0   70      1
# amc rebel sst              16         8          304        150   3433         12.0   70      1
# ford torino                17         8          302        140   3449         10.5   70      1
# ford galaxie 500           15         8          429        198   4341         10.0   70      1
# chevrolet impala           14         8          454        220   4354          9.0   70      1
# plymouth fury iii          14         8          440        215   4312         70.5   70      1
# pontiac catalina           14         8          455        225   4425         10.0   70      1
# amc ambassador dpl         15         8          390        190   3850          8.5   70      1
# dodge challenger se        15         8          383        170   3563         10.0   70      1
# plymouth 'cuda 340         14         8          340        160   3609          8.0   70      1
# chevrolet monte carlo      15         8          400        150   3761          9.5   70      1
# buick estate wagon (sw)    14         8          455        225   3086         10.0   70      1

Max Acceleration

mask <- matrixAuto[,"acceleration"] == max_vals_matrix["acceleration"]
matrixAuto[mask,]
#                              mpg cylinders displacement horsepower weight acceleration year origin
# plymouth fury iii             14         8          440        215   4312         70.5   70      1
# volkswagen 1131 deluxe sedan  26         4           97         46   1835         70.5   70      2

Max Horsepower

mask <- matrixAuto[,"horsepower"] == max_vals_matrix["horsepower"]
matrixAuto[mask,]
#                         mpg cylinders displacement horsepower weight acceleration year origin
# pontiac catalina         14         8          455        225   4425           10   70      1
# buick estate wagon (sw)  14         8          455        225   3086           10   70      1

Multiple aggregation

agg_vals_matrix <- apply(matrixAuto, 2, function(col) c(max=max(col), min=min(col)))

mask <- matrixAuto[,"horsepower"] == agg_vals_matrix["min", "horsepower"]
matrixAuto[mask, drop=FALSE]   
#                              mpg cylinders displacement horsepower weight acceleration year origin
# volkswagen 1131 deluxe sedan  26         4           97         46   1835         70.5   70      2

row.names(matrixAuto[mask,drop=FALSE])
# [1] "volkswagen 1131 deluxe sedan"

# NOTE: drop PREVENTS ONE-ROW MATRIX CONVERTED INTO VECTOR

In fact, you can handle same operation with original data frame via aggregate:

Dataframe Aggregation

max_agg_df <- aggregate(cbind(mpg, cylinders, displacement, horsepower, weight,
                              acceleration, year, origin) ~ ., data=myfile[num_cols], FUN=max)
    max_agg_df 
#   mpg cylinders displacement horsepower weight acceleration year origin
# 1  27         8          455        225   4425         70.5   70      3

Max Cylinders

myfile[myfile$cylinder == max_agg_df$cylinder,]
#    mpg cylinders displacement horsepower weight acceleration year origin                      name
# 1   18         8          307        130   3504         12.0   70      1 chevrolet chevelle malibu
# 2   15         8          350        165   3693         11.5   70      1         buick skylark 320
# 3   18         8          318        150   3436         11.0   70      1        plymouth satellite
# 4   16         8          304        150   3433         12.0   70      1             amc rebel sst
# 5   17         8          302        140   3449         10.5   70      1               ford torino
# 6   15         8          429        198   4341         10.0   70      1          ford galaxie 500
# 7   14         8          454        220   4354          9.0   70      1          chevrolet impala
# 8   14         8          440        215   4312         70.5   70      1         plymouth fury iii
# 9   14         8          455        225   4425         10.0   70      1          pontiac catalina
# 10  15         8          390        190   3850          8.5   70      1        amc ambassador dpl
# 11  15         8          383        170   3563         10.0   70      1       dodge challenger se
# 12  14         8          340        160   3609          8.0   70      1        plymouth 'cuda 340
# 13  15         8          400        150   3761          9.5   70      1     chevrolet monte carlo
# 14  14         8          455        225   3086         10.0   70      1   buick estate wagon (sw)

Max Acceleration

myfile[myfile$acceleration == max_agg_df$acceleration,]
#    mpg cylinders displacement horsepower weight acceleration year origin                         name
# 8   14         8          440        215   4312         70.5   70      1            plymouth fury iii
# 20  26         4           97         46   1835         70.5   70      2 volkswagen 1131 deluxe sedan

Max Horsepower

myfile[myfile$horsepower == max_agg_df$horsepower,]
#    mpg cylinders displacement horsepower weight acceleration year origin                    name
# 9   14         8          455        225   4425           10   70      1        pontiac catalina
# 14  14         8          455        225   3086           10   70      1 buick estate wagon (sw)

Multiple Aggregation

agg_raw <- aggregate(cbind(mpg, cylinders, displacement, horsepower, weight,
                          acceleration, year, origin) ~ ., data=myfile[num_cols], 
                     FUN=function(col) c(max=max(col), min=min(col)))
agg_df <- do.call(data.frame, agg_raw)
                        
myfile[myfile$horsepower == agg_df$horsepower.min,]
#    mpg cylinders displacement horsepower weight acceleration year origin                         name
# 20  26         4           97         46   1835         70.5   70      2 volkswagen 1131 deluxe sedan
                        
myfile$name[myfile$horsepower == agg_df$horsepower.min]
# [1] "volkswagen 1131 deluxe sedan"

Online Demo

Parfait
  • 104,375
  • 17
  • 94
  • 125
  • Still can't retrieve the rowname for min horsepower which I had originally asked. – Riten Bhagra Jul 22 '20 at 16:29
  • Simply call `row.names` on filtered matrix, `row.names(matrixAuto[mask,])` or return data frame *name* column, `myfile$name[myfile$horsepower == max_agg_df$horsepower]`. Examples above returns whole matrix and data frame. Call whatever attribute or column slice as needed. As for min of horsepower, see edits with new section for multiple aggregation for both `max` and `min` of *all* number columns with `row.names` and column slice. – Parfait Jul 22 '20 at 18:33