Assume we have a dataframe
x y
1 1
2 4
4 5
how can you add a new variable to the dataframe such that if x is less than or equal to 1 it returns “good” if x is between 3 and 5 it returns “bad” else returns “fair”
x y w
1 1 "good"
2 2 "fair"
5 5 "bad"
Applied the method shown by ocram., however this one here does not work.
d1 <- c("e", "c", "a")
d2 <- c("e", "a", "b")
w <- ifelse(d1 == "e" & (d2=="e"), 1, ifelse((d1 == "a") & (d2 =="b"), 2, ifelse(d1 == "e"),3,99))
Any ideas?
Thanks
One obvious and straightforward possibility is to use “if-else conditions”. In that example
** For the additional question in the edit**
Is that what you expect ?
If you do not feel comfortable with the
ifelsefunction, you can also work with theifandelsestatements for such applications.