0

I have an array vals that contains values between 0 and 1 and I also have defined a threshold, th

vals <-  c(0.0374250747263432, 0.00394217809662223, 0.0719008445739746, 
    0.0930692106485367, 0.0615200139582157)

th <- 0.5

How can I create a second vector vals_bnry whose elements are values of 0 or 1 based on the size of the corresponding elements in the vals vector?

Example: if vals[1] > 0.5 -> vals_bnry[1] = 1

Is there a way to avoid the for loop?

Captain Hat
  • 2,444
  • 1
  • 14
  • 31
s28
  • 173
  • 5

1 Answers1

2

Use the function base::ifelse(), like so:

ifelse(vals > th, 1, 0)
Captain Hat
  • 2,444
  • 1
  • 14
  • 31