1

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?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Anys
  • 21
  • 7
  • Welcome to Stackoverflow. Try `x=c(....),`, `lb=0`, `ub=10`, before you define your function, and don't name your data the same as your function. When you entered your function (second use of recode.numeric), it overwrote your recode.numeric(this is my data). – Chris Jul 29 '21 at 13:44
  • Well, maybe I wrote it wrong. I haven't got any dataset over here. I tried to write function recode.numeric recode.numeric <- function(.... If it would be ok, I could write thisrecode.numeric(x = c(5, 3, -5, 4, 3, 97),lb = 0, ub = 10) and I'll have back this c(5, 3, NA, 4, 3, NA). But it's not ok. – Anys Jul 29 '21 at 15:18
  • It gave me this: Warning messages: 1: In if (ub > x) { : the condition has length > 1 and only the first element will be used 2: In if (x > lb) { : the condition has length > 1 and only the first element will be used 3: In if (ub < x) { : the condition has length > 1 and only the first element will be used 4: In if (x < lb) { : the condition has length > 1 and only the first element will be used – Anys Jul 29 '21 at 15:22

0 Answers0