18

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

1
  • 2
    Your last code is not working since you've messed up parentheses; it should be always ifelse(cond,ifTrue,ifFalse), never ifelse(cond) ifTrue, ifFalse. Commented Apr 20, 2011 at 12:59

2 Answers 2

24

One obvious and straightforward possibility is to use "if-else conditions". In that example

x <- c(1, 2, 4)
y <- c(1, 4, 5)
w <- ifelse(x <= 1, "good", ifelse((x >= 3) & (x <= 5), "bad", "fair"))
data.frame(x, y, w)

** For the additional question in the edit** Is that what you expect ?

> 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)))
>     
> data.frame(d1, d2, w)
  d1 d2  w
1  e  e  1
2  c  a 99
3  a  b  2

If you do not feel comfortable with the ifelse function, you can also work with the if and else statements for such applications.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a tonne, however the approach is only limited to 2 conditions? see the edited question above
4

If you have a very limited number of levels, you could try converting y into factor and change its levels.

> xy <- data.frame(x = c(1, 2, 4), y = c(1, 4, 5))
> xy$w <- as.factor(xy$y)
> levels(xy$w) <- c("good", "fair", "bad")
> xy
  x y    w
1 1 1 good
2 2 4 fair
3 4 5  bad

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.