Home > Net >  R programming, Ifelse Error: "Error in ifelse(i %in% c(1, 6), rasterImage(ringIMG2, -22, -22, 2
R programming, Ifelse Error: "Error in ifelse(i %in% c(1, 6), rasterImage(ringIMG2, -22, -22, 2

Time:02-14

I am trying to run different versions of the same command in a for loop based on the value of i.

If i equals 1 or 6 run this: rasterImage(ringIMG2, -22, -22, 22, 22, interpolate=FALSE, angle =0)

If i equals 2, 3 or 5 run this: rasterImage(ringIMG2, -22, -22, 22, 22, interpolate=FALSE, angle =0)

If i equals 4 run this: rasterImage(ringIMG2, -23.5, -23.5, 23.5, 23.5, interpolate=FALSE, angle =0)

I am using a nested ifelse statement.

  for (i in 1:6)
  {
    #adjusts size of image within the plot
    ifelse(i %in% c(1,6), rasterImage(ringIMG2, -22, -22, 22, 22, interpolate=FALSE, angle =0),
           ifelse(i %in% c(2,3,5), rasterImage(ringIMG2, -23, -23, 23, 23, interpolate=FALSE, angle =0),
                  rasterImage(ringIMG2, -23.5, -23.5, 23.5, 23.5, interpolate=FALSE, angle =0)
           )
    )
    }

I keep getting the following error:

Error in ifelse(i %in% c(1, 6), rasterImage(ringIMG2, -22, -22, 22, 22,  : 
  replacement has length zero
In addition: Warning messages:
1: In readPNG(paste0("R", i, ".png")) :
  libpng warning: iCCP: known incorrect sRGB profile
2: In rep(yes, length.out = length(ans)) :
  'x' is NULL so the result will be NULL

The code on its own is running fine:

rasterImage(ringIMG2, -22, -22, 22, 22, interpolate=FALSE, angle =0)

Well I'm still getting the first warning but that is not any real concern.

I have tried the simplifying and it seems to work...

check <- ifelse(i %in% c(1,6), print('Yes'))
[1] "Yes"

My syntax looks ok based on the link below:

Nested ifelse syntax

ifelse(<condition>, <yes>,  
              ifelse(<condition>, <yes>, <no>)
                    )
       )

Anyone got an idea where I'm going wrong?

Thanks!

CodePudding user response:

You want to use if and else — not to be confused with ifelse().

ifelse() is useful when you want to create a vector of values based on a vector of conditions. It returns a vector the same length as the test condition, and expects all arguments to be the same length. But rasterImage() doesn’t have a return value — passing it as an argument to ifelse() is equivalent to passing NULL. length(i) != length(NULL), so you get an error.

But if and else are standard control flow statements, rather than functions with prescribed inputs and outputs, so will work better for your case:

for (i in 1:6) {
  if (i %in% c(1,6)) {
    rasterImage(ringIMG2, -22, -22, 22, 22, interpolate=FALSE, angle =0)
  } else if (i %in% c(2,3,5)) {
    rasterImage(ringIMG2, -23, -23, 23, 23, interpolate=FALSE, angle =0)
  } else {
    rasterImage(ringIMG2, -23.5, -23.5, 23.5, 23.5, interpolate=FALSE, angle =0)
  }
}

You could also accomplish this a bit more concisely without if statements:

for (i in c(22, 23, 23, 23.5, 23, 22)) {
  rasterImage(ringIMG2, -i, -i, i, i, interpolate=FALSE, angle =0)
}
  • Related