Home > Enterprise >  selecting multiple max values from multiple ranges
selecting multiple max values from multiple ranges

Time:01-17

I'm looking to find multiple max values using multiple ranges from a single table without using a loop.

It's difficult to explain, but here's an example:

list of value <- c(100, 110, 54, 64, 73, 23, 102)
beginning_of_max_range <- c(1, 2, 4)
end_of_max_range <- c(3, 5, 6)

output

110, 110, 73

  1. max(100, 110, 54)
  2. max(110, 54, 64)
  3. max(64, 73, 23)

CodePudding user response:

You may do this with mapply -

list_of_value <- c(100, 110, 54, 64, 73, 23, 102)
beginning_of_max_range <- c(1, 2, 4)
end_of_max_range <- c(3, 5, 6)

mapply(function(x, y) max(list_of_value[x:y]), beginning_of_max_range, end_of_max_range)
#[1] 110 110  73

We create a sequence from beginning_of_max_range to end_of_max_range, subset it from list_of_value and get the max from each pair.

  •  Tags:  
  • Related