indexing - Index maximum of duplicate maximum values in R -
i know which.max able tell me index of first occurrence of maximum value, want obtain maximum index value of indexes duplicate maximum values in given range.
below data working with.
#given data set dataset1 <- data.frame(index=1:6, value=c(.456,.92,.88,.92,.88,0.85)) dataset1 # index value 1 1 0.456 2 2 0.920 3 3 0.880 4 4 0.920 5 5 0.880 6 6 0.850 #-------------------------- #create filtered combinations n=nrow(dataset1) comb<-data.frame(t(combn(seq(1:(n)), 2))) library("plyr") library("dplyr") fcomb<-comb %>% filter(x2-x1>1) %>% select(x1,x2) fcomb.mat<-as.matrix(fcomb) #filtered combination set matrix colnames(fcomb.mat)[1:2] <- c("ind1","ind2") #-------------------------- #finding maximum value original dataset in range between "ind1" & "ind2" fun <- function(x,y) max(dataset1$value[(.x <- x:y)[-c(1, length(.x))]]) max.val = as.matrix(mapply(fun, fcomb.mat[,1], fcomb.mat[,2]))
problem:
i obtain last column "indexmax" shown below.
for example ind1=1 & ind2=5 (i.e row #3), max flow value dataset1 between index=1:5 0.920. index of maximum value can either 2 or 4, choose 4 maximum value of indexes.how implement this?
final # ind1 ind2 max.val indexmax 1 1 3 0.92 2 2 1 4 0.92 2 3 1 5 0.92 4 4 1 6 0.92 4 5 2 4 0.88 3 6 2 5 0.92 4 7 2 6 0.92 4 8 3 5 0.92 4 9 3 6 0.92 4 10 4 6 0.88 5
thanks in advance
i figured out myself. in case of use else in future:
fun2 <- function(x,y) max((which(dataset1[,2][(.x <- x:y)][-c(1,length(.x))]==max(dataset1[,2][(.x <- x:y)][-c(1, length(.x))])))+x) max.pos=as.matrix(mapply(fun2, fcomb.mat[,1],fcomb.mat[,2])) final<-data.frame(fcomb.mat,max.val,max.pos)
Comments
Post a Comment