GRS - 1 year ago 84

R Question

Let's say I have a vector $(0,1,2,3,4,5)$.

I want to transform it into the following: if the value in the original vector is:

$=0 \rightarrow 0$

$> 0$ but $<5 \rightarrow 1$

$=5 \rightarrow 2$

I tried:

`v <- c(0,1,2,3,4,5)`

v <- as.numeric(v=0, v>0 & v<5, v=5)

Answer Source

You can use two logical operations and add the results:

```
v2 <- (v > 0) + (v >= 5)
# [1] 0 1 1 1 1 2
```