0

I would like to be able to choose the max value from each comparison in a vector. I have two vectors:

probsA<-posttheta_A^R*(1-posttheta_A)^NR
#2.634872e-02 6.709075e-03 1.107573e-04 1.708307e-03 2.820171e-05

probsB<-posttheta_B^R*(1-posttheta_B)^NR
#0.0013311712 0.0012295459 0.0009688963 0.0011356790 0.0008949280
choice<-max(probsA,probsB)
0.02634872

the max function is searching for the biggest out of 10 values and prints it. I would like it to compare each element in each vector with its corresponding element and print 5 results.

Scavenger23
  • 209
  • 1
  • 6

2 Answers2

1

You can use Map. Map applies a function to the corresponding elements of given vectors.

probsA <- c(2.634872e-02, 6.709075e-03, 1.107573e-04, 1.708307e-03, 2.820171e-05)
probsB <- c(0.0013311712, 0.0012295459, 0.0009688963, 0.0011356790, 0.0008949280)


Map(max, probsA, probsB)

[[1]]
[1] 0.02634872

[[2]]
[1] 0.006709075

[[3]]
[1] 0.0009688963

[[4]]
[1] 0.001708307

[[5]]
[1] 0.000894928
phiver
  • 23,048
  • 14
  • 44
  • 56
1

We can just use pmax

pmax(probsA, probsB)
#[1] 0.0263487200 0.0067090750 0.0009688963 0.0017083070 0.0008949280

data

probsA <- c(2.634872e-02, 6.709075e-03, 1.107573e-04, 1.708307e-03, 2.820171e-05)
probsB <- c(0.0013311712, 0.0012295459, 0.0009688963, 0.0011356790, 0.0008949280)
akrun
  • 874,273
  • 37
  • 540
  • 662