2

I have a dataframe and I would like to create a columns based on multiple conditions:

v1  v2  v3  v4  v5
4   1   2   4   5
5.5 2   4   5   6
21  9  20  30   50 
6   4   5   7   9
10  3   6   5   9

Basically, create v6 with following possible values: Cat, dog, ant, hog

  1. If v1 is between v2 and v3, then cat
  2. If v1 is between v3 and v4, then dog
  3. If v1 is between v4 and v5, then ant
  4. If v1 is greater than v5, then hog.

I know how to do this for a single condition, but not multiple conditions.

maximusdooku
  • 5,242
  • 10
  • 54
  • 94

2 Answers2

7

Nested ifelse statements:

df$v6 <- with(df, ifelse(
  v1 >= v2 & v1 <= v3, 'cat', ifelse(
  v1 >= v3 & v1 <= v4, 'dog', ifelse(
  v1 >= v4 & v1 <= v5, 'ant', ifelse(
  v1 >= v5, 'hog', 'whoops')))))


> df
    v1 v2 v3 v4 v5  v6
1  4.0  1  2  4  5 dog
2  5.5  2  4  5  6 ant
3 21.0  9 20 30 50 dog
4  6.0  4  5  7  9 dog
5 10.0  3  6  5  9 hog
Zelazny7
  • 39,946
  • 18
  • 70
  • 84
  • Thank you. It works...I would like to do a summary(df$v6) but I don't get any results out of it. Basically, to check whether all values have been assigned one of the four values... I tried to convert to numeric, but I am getting all NAs after that...Any idea how to check? I have thousands of values... – maximusdooku Jan 06 '16 at 21:11
  • I would do this `ftable(df$v1, df$v6)` – Zelazny7 Jan 06 '16 at 21:12
0

Assuming d is your data frame, you can try something like below using ifelse function.

d[,6] <- rep(" ", 5)

d[,6] <-  ifelse(d[,1] >= d[,2] & d[,1] <= d[,3] & d[,6] == " ", "cat", d[,6])
d[,6] <-  ifelse(d[,1] >= d[,3] & d[,1] <= d[,4] & d[,6] == " ", "dog", d[,6])
d[,6] <-  ifelse(d[,1] >= d[,4] & d[,1] <= d[,5] & d[,6] == " ", "ant", d[,6])
d[,6] <-  ifelse(d[,1] >= d[,5] & d[,6] == " ", "hog", d[,6])
user353gre3
  • 2,747
  • 4
  • 24
  • 27