'Recode values by function()

I have one quest (pretty short). I shoud recode variebles with function(). I tried some, but it doesn't work still. It should work with this:

recode.numeric(x = c(5, 3, -5, 4, 3, 97),lb = 0, ub = 10)

And turn this call to c(5, 3, NA, 4, 3, NA)

My try are these:

recode.numeric <- function(x, lb, ub){
  if(ub > x){x=x}
  if (x > lb){x=x}
  if(ub < x){x="NA"}
  if (x<lb) {x="NA"}
}

So, what am I doing wrong?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source