1
a <- c(-1, 0, 2, 3)
b <- c(0, 2, 9, -3)

I have 2 vectors here of equal length. I want to find the value that is the maximum of the 2 vectors element-wise. That is, I want to compare the first element of a and b and find the bigger value, then compare the second, third, etc. I want the final vector to be 0, 2, 9, 3.

Adrian
  • 9,229
  • 24
  • 74
  • 132

1 Answers1

2

We can use pmax in base R

pmax(a, b, na.rm = TRUE)
#[1] 0 2 9 3
akrun
  • 874,273
  • 37
  • 540
  • 662