0

I changed my function but still the else statement does not seem to work... Any thoughts on why the else is not working.

 determine_range <- function() {

 values <- c(1001, 5001, 10001)
 data <- data.frame(x = values)  
 colnames(data) <- "BPM_BEDRAG"

 ranges <- c(1000, 5000, 10000, 15000, 20000, 25000)

 print(data$BPM_BEDRAG)  
 for(i in 1:nrow(data)){


  if (data$BPM_BEDRAG[i] > ranges[1] && data$BPM_BEDRAG[i] < ranges[2] ) {data$BPM_BEDRAG[i] <- "Catagory 1"
  } else if (data$BPM_BEDRAG[i] > ranges[2] |data$BPM_BEDRAG[i] < ranges[3]  ) {data$BPM_BEDRAG[i] <- "Catagory 2"
  } else data$BPM_BEDRAG[i] <- "Catagory 3"   
  }
  data$BPM_BEDRAG

  }
Marc van der Peet
  • 323
  • 1
  • 6
  • 14
  • Well, the duplicate explains why this is wrong, but it would be better if you avoided the loop and just used `cut()`. See the `?cut` help page for more info. – MrFlick Jul 03 '15 at 15:23
  • @MrFlick Thanks for your answer but I dont get what is exactly causing the fault. Could you eloborate on what goes wrong? – Marc van der Peet Jul 03 '15 at 15:34
  • Your `else` needs to start on the same line as the closing `]` of your `if` block. But doing a loop like this is completely unnecessary. (The title of your question however is explicitly asking about `else` blocks.) – MrFlick Jul 03 '15 at 15:39
  • @MrFlick sorry to bother you again. But if I look at the duplicate question and edit it (see above) the else still does not seem to work. Any thoughts? – Marc van der Peet Jul 03 '15 at 15:57
  • Your second `if` has a `|` (or) rather than an `&` (and). It's just a logic problem now. – MrFlick Jul 03 '15 at 16:04
  • Apologies youre right. But when I replace the | with && I still get "Catagory 1" "Catagory 3" "Catagory 1" – Marc van der Peet Jul 03 '15 at 16:14

0 Answers0