# Using conditional statements on raster not working in R

I'm trying to do this thing where I'm classifying climate and R just seems to not like it when I do if/and functions I don't even know someone please help me

So I created a new raster entitled "clim" with a value for each of the climate types I'm trying to create. I used this function and R like won't let me do it even though I researched conditional statements and it looks like I'm doing everything correctly.

Basically, I want my "clim" to have the value of "2" if the average temperature of the hottest month is 5 to 12 degrees celsius, the average temperature of the coldest month is 2 to -8 celsius, and the annual temperature range is less than 13 degrees celsius. So I used this function:

`if(isTRUE((tmax > 5) && (tmax < 12) && (tmin < 2) && (tmin > -8) && (amp < 13))) {clim <- 2}`

And then R told me: `Error in (tmax > 5) && (tmax < 12) : invalid 'x' type in 'x && y'`

What does that mean?

I really did everything according to the instructions of how to use a conditional function, R has no reason to treat me like this.

An `if` clause in R is not the way to conditionally replace values in a vector. What you want to do is set the values in `clim` that match the condition to "2". For the simple case with just `tmax`:
``````clim[tmax>5 & tmax < 12] = 2
Note the use of single `&` here. This compares all elements of the operands (`tmax` and "5" for example in the first) and returns a thing of the same size as `tmax` with `TRUE` and `FALSE` values.
Double `&&` only looks at the first value and is used in `if` statements since `if`s are for control flow only, and the code either runs the `if` block or not.
That should give you enough to fix your code line. Basically, replace matching values in `clim`, and single `&` for conditions in vectors.
• This is a fantastic explanation of if clauses and conditional statements! I would also point out that terra has `ifel` , emulating `ifelse`, that provides a convenient wrapper function for this type of operator. Although, please note the comment on "syntactic sugar" and other approaches to the same ends. rdocumentation.org/packages/terra/versions/0.2-10/topics/ifel Jun 21 '21 at 15:37