2

Problem

Notice that when I run the code on Computer 2 (R version 4.2.0)

if(c(1,2,3,4) == 1){ print("Hi") }

it is going straight to error. Is there any way to make it automatically use the first element like in Computer 1 (R version 4.1.2)?

On Computer 1 with R version 4.1.2 enter image description here

On Computer 2 with R version 4.2.0 enter image description here

Ian
  • 51
  • 4
  • 1
    But what if I want it to only consider the first element in a vector like it previously did? I recognize I could just do c(1,2,3,4)[1], but I have previous code already written out and don't want to make this change for everything. – Ian May 02 '22 at 18:15
  • 5
    From R 4.2.0 release news: *"Calling if() or while() with a condition of length greater than one gives an error rather than a warning."* So you need to either change the code as akrun suggests (to only check the first value use `if(c(1, 2, 3, 4)[1] == 1)`) or install an older version of R or Computer 2. – Gregor Thomas May 02 '22 at 18:15
  • 1
    Bummer. Ok thank you for clearing that up! – Ian May 02 '22 at 18:16
  • 2
    I anticipate more questions like this as R-4.2 becomes more used. I agree with its change in logic from a warning to an error, but not everybody follows the "NEWS" as closely nor understands the concerns with its behavior pre-4.2.0. The dupe links provided point to potential ways around the problem (of length > 1 in an `if` conditional), so ignore their mention of "warning" and skip right to the remediation suggestions, including `ifelse` or `isTRUE` or `[1]`. – r2evans May 02 '22 at 18:55

0 Answers0