I have a numeric vector:
p = c(0.69, 0.43, 0.72, 0.62, 0.29, 0.7)
I wish to compare the values to a limit, e.g. 0.5. If the values are equal or larger than the cut-off, the result should 1. Values less than 0.5 should become 0.
I tried ifelse
, but it errors:
newvector = rep(0, length(p))
newvector = ifelse(p >= 0.5, newvector = 1, newvector = 0)
# Error in ifelse(p >= 0.5, newvector = 1, newvector = 0) :
# unused arguments (newvector = 1, newvector = 0)
You don't need to make newvector
beforehand and using it as an argument to ifelse
is causing the error.
Try this...
newvector <- ifelse( p>=0.5 , 1 , 0 )
But even better just do it vectorised...
newvector <- as.integer( p >= 0.5 )
Just use :
newvector <- ifelse(p>=0.5,1,0)
The second and third arguments of ifelse
are not expressions but the values to be returned.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With