Using `&` and `|` in `for` statements

Hi,

I've read in various places that using & and | within for statements should not really work. For example, as illustrated here. However, I have a bit of code for my shinyapps here:

`
bridgingTable <- reactive({
if(input$thromRisk == "High" & input$gfr >= 30){
bridge <- BrHighRisk

  # To sort out the Date and Weekday Columns
  opdate <- as.Date(input$OpDate)
  dates <- as.Date(c(opdate - 6, opdate - 5, opdate - 4, opdate -3, opdate - 2, opdate - 1, opdate, opdate + 1, opdate + 2))
  dates_chr <- format(as.Date(c(opdate - 6, opdate - 5, opdate - 4, opdate -3, opdate - 2, opdate - 1, opdate, opdate + 1, opdate + 2)), "%d/%m/%y")
  dates_chr <- as.character(dates_chr)
  bridge$Date <- dates_chr
  wdays <- weekdays(dates)
  bridge$Weekday <- wdays
  
  if(input$weight <= 46){
    bridge <- bridge %>% 
      select(Day, Date, Weekday, Warfarin, "INR Check" = INR, "Dalteparin Treatment" = TxDose_1,  "Dalteparin Prophylaxis*" = Prophylaxis_1)
  }else if(input$weight > 46 & input$weight < 57){
    if(input$weight < 50){
      bridge <- bridge %>%
        select(Day, Date, Weekday, Warfarin, "INR Check" = INR, "Dalteparin Treatment" = TxDose_2, "Dalteparin Prophylaxis*" = Prophylaxis_1)
    }else if(input$weight >= 50 & input$weight < 100){
      bridge <- bridge %>%
        select(Day, Date, Weekday, Warfarin, "INR Check" = INR, "Dalteparin Treatment" = TxDose_2, "Dalteparin Prophylaxis*" = Prophylaxis_2)
    }
  }else if(input$weight >= 57 & input$weight < 69){
    bridge <- bridge %>%
      select(Day, Date, Weekday, Warfarin, "INR Check" = INR, "Dalteparin Treatment" = TxDose_3, "Dalteparin Prophylaxis*" = Prophylaxis_2)
  }else if(input$weight >= 69  & input$weight < 83){
    bridge <- bridge %>%
      select(Day, Date, Weekday, Warfarin, "INR Check" = INR, "Dalteparin Treatment" = TxDose_4, "Dalteparin Prophylaxis*" = Prophylaxis_2)
  }else if(input$weight >= 83 & input$weight < 100){
    bridge <- bridge %>%
      select(Day, Date, Weekday, "Warfarin Dose" = Warfarin, "INR Check" = INR, "Dalteparin Treatment" = TxDose_5, "Dalteparin Prophylaxis*" = Prophylaxis_2)
  }else if(input$weight >= 100 & input$weight < 150){
    bridge <- bridge %>%
      select(Day, Date, Weekday, "Warfarin Dose" = Warfarin, "INR Check" = INR, "Dalteparin Treatment" = TxDose_5, "Dalteparin Prophylaxis*" = Prophylaxis_3)
  }else if(input$weight >= 150){
    bridge <- bridge %>%
      select(Day, Date, Weekday, "Warfarin Dose" = Warfarin, "INR Check" = INR, "Dalteparin Treatment" = TxDose_5, "Dalteparin Prophylaxis*" = Prophylaxis_4)
  }
  
}else if(input$thromRisk == "Moderate" & input$gfr >= 30){
  bridge <- BrModRisk

`
This seems to work okay without any errors being generated when it runs.

I guess I shouldn't really question why something would work but I am just a bit puzzled and wouldn't mind some explanation as I am still relatively new to coding.

KR,

L

just a comment first ... I don't quite agree that

using & and | within for statements should not really work.

As stated here (emphasis mine): https://stat.ethz.ch/R-manual/R-devel/library/base/html/Logic.html

& and && indicate logical AND and | and || indicate logical OR. The shorter form performs elementwise comparisons in much the same way as arithmetic operators. The longer form evaluates left to right examining only the first element of each vector. Evaluation proceeds only until the result is determined. The longer form is appropriate for programming control-flow and typically preferred in if clauses.

So, if you want element wise comparison (which you may or may not want in a for loop) use & and |. In my experience, I would use the longer form only with scalars because otherwise it is a bit implicit what's going on. As I see it for scalars both forms do the same, but for clarity of the code I would use the longer form as a signal that I intend to compare scalars (and not vectors).

3 Likes

Hi Valeri,

Thanks for taking the time to reply. I think I have some vague idea now.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.