[英]retrieving the row name of max value in a matrix in r programming
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但是后来我尝试了同样的方法来计算最大加速度,并且使用 max function 并且当我尝试使用时只返回一个值
maxAccelaration=max(acceleration)==acceleration
rownames(matrixAuto[maxAccelaration,])
NULL
value was returned. NULL
值已返回。 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?你能解释一下为什么我会得到正确的答案多个最大值而 null 值使用我的代码单个最大值? 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
我正在尝试获取最大加速汽车的行名,即大众 1131 豪华轿车
Any help is appreciated.任何帮助表示赞赏。
my file using the dput() function is我使用 dput() function 的文件是
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")
When subsetting a matrix (or dataframe) with only 1 row, it will return a named vector (no rownames).当对只有 1 行的矩阵(或数据框)进行子集化时,它将返回一个命名向量(无行名)。 When you subset more than 1 row, you will get a matrix (or dataframe) back.
当您对超过 1 行进行子集化时,您将获得一个矩阵(或数据框)。
Note, that is you use tibble::tibble
and subset one row, it will return a tibble
.请注意,即您使用
tibble::tibble
和子集一行,它将返回一个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, ])
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))
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
:事实上,您可以通过
aggregate
处理与原始数据帧相同的操作:
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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.