Finding the Index of a Max Value in R

Finding index corresponding to the maximum value

which.max works well with dplyr grouping:

library(dplyr)

Data %>% group_by(Order_Year) %>%
summarise(by.Yen = Ship_Year[which.max(Yen)],
by.Units = Ship_Year[which.max(Units)])

## # A tibble: 4 × 3
## Order_Year by.Yen by.Units
## <dbl> <dbl> <dbl>
## 1 1999 NA NA
## 2 2000 1997 1997
## 3 2001 1998 1998
## 4 2002 1997 1997

finding the index of a max value in R

If you have 2 data.points at the maximum, which.max will only refer to the first one. A more complete solution would involve rank:

# data with a tie for max  
surge <- data.frame(MeshID=c(1:7,1:4),StormID=c(rep(1412,7),
rep(1413,4)),Surge=c(0.01,0.03,0.09,0.12,0.02,0.02,0.07,0.06,0.02,0.05,0.06))

# compute ranks
surge$rank <- ave(-surge$Surge,surge$StormID,FUN=function(x) rank(x,ties.method="min"))
# subset on the rank
subset(surge,rank==1)
MeshID StormID Surge rank
4 4 1412 0.12 1
8 1 1413 0.06 1
11 4 1413 0.06 1

R: how to find the indices of the maximal value per column

transpose mat and use max.col

i = max.col(t(mat))
v = t(mat)[cbind(seq(NCOL(mat)), i)]
v
# [1] 7 8 6
i
# [1] 2 2 3

How to find the position of the maximum value in a list in R?

It's little mess, but it will give you location of maximum value

library(stringr)
library(dplyr)

x <- which.max(unlist(test))
y <- str_split(names(x), "", simplify = T)
a <- y[1]

z <- sapply(test[[a]], length) %>% cumsum
b <- which(z > as.numeric(y[2]))
c <- as.numeric(y[2]) - z[b-1]

test[[a]][[b]][[c]]

[1] 2094234

In this code, a, b, and c indicate location. If you need more generalized version, please let me know

Finding the index of the maximum value by group for a data.table in R

Using data.table:

#dat <- as.data.table(dat)
#dat$Date <- as.Date(dat$Date,format="%m/%d/%Y")
dat[dat[, Date != max(Date) , by=Person][,V1], Date := NA]
dat

# Name Person Date
#1: A 1 2004-01-01
#2: A 2 <NA>
#3: A 2 2004-01-03
#4: A 3 <NA>
#5: A 3 <NA>
#6: A 3 2004-01-09
#7: B 4 2004-01-07
#8: B 5 <NA>
#9: B 5 2004-01-10
#10: B 6 <NA>
#11: B 6 <NA>
#12: B 6 2004-01-17

How to find the index of the min/max value in a sparse matrix in R?

I'm just gonna set up an example:

require(Matrix)

A <- matrix(1:100, nrow=10) %% 15
A[A < 7] <- 0
A <- Matrix(A, sparse=T)

Yep, this is a sparse matrix:

> A
10 x 10 sparse Matrix of class "dgCMatrix"

[1,] . 11 . . 11 . . 11 . .
[2,] . 12 7 . 12 7 . 12 7 .
[3,] . 13 8 . 13 8 . 13 8 .
[4,] . 14 9 . 14 9 . 14 9 .
[5,] . . 10 . . 10 . . 10 .
[6,] . . 11 . . 11 . . 11 .
[7,] 7 . 12 7 . 12 7 . 12 7
[8,] 8 . 13 8 . 13 8 . 13 8
[9,] 9 . 14 9 . 14 9 . 14 9
[10,] 10 . . 10 . . 10 . . 10

Logical tests on it work just fine

> A == max(A)
10 x 10 sparse Matrix of class "lgCMatrix"

[1,] . : . . : . . : . .
[2,] . : : . : : . : : .
[3,] . : : . : : . : : .
[4,] . | : . | : . | : .
[5,] . . : . . : . . : .
[6,] . . : . . : . . : .
[7,] : . : : . : : . : :
[8,] : . : : . : : . : :
[9,] : . | : . | : . | :
[10,] : . . : . . : . . :

And we can get row & column indices of max(A) no problem:

> which(A == max(A), arr.ind=T)
row col
[1,] 4 2
[2,] 9 3
[3,] 4 5
[4,] 9 6
[5,] 4 8
[6,] 9 9

I would suggest that you figure out which of these steps isn't giving you the output it should.



Related Topics



Leave a reply



Submit