Convert numeric vector to binary (0/1) based on limit
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.
You could also use findInterval
for this particular case:
findInterval(p, .5)