12

I have got a dataframe to which I want to append a column with values depending on a column in my dataframe. My dataframe looks somewhat like this:

c1  c2  c3
x   2   z
y   5   f
c   3   r
a   11  z

Now I want to append another column c4 based on the values of c2. For all values between 0 and 4 I want to append "low", for values between 5 and 9 I want to append "medium" and for those bigger than 10 "high".

c1  c2  c3  c4
x   2   z   "low"
y   5   f   "medium"
c   3   r   "low"
a   11  z   "high"

Probably the answer is quite simple, but I really can't think of something.

zx8754
  • 52,746
  • 12
  • 114
  • 209
Ramona
  • 335
  • 1
  • 5
  • 18
  • Look at this link: https://stackoverflow.com/questions/15016723/how-to-add-column-into-a-dataframe-based-on-condition-in-r-programming – SatZ Jun 22 '18 at 13:00

2 Answers2

18

You can nest ifelse stataments. It's very handy when you are making categorical variables out of continuous ones.

data$c4 <- ifelse(data$c2 >= 0 & data$c2 <= 4, 'low',
                  ifelse(data$c2 >=5 & data$c2 <=9, 'medium',
                         ifelse(data$c2 >=10, 'High', 'something else')
divibisan
  • 11,659
  • 11
  • 40
  • 58
Puddlebunk
  • 493
  • 3
  • 10
  • 1
    Thanks I didn't know ifelse() can be nested! – Ramona Jun 22 '18 at 13:05
  • @Ramona, yes. very handy when you are making categorical variables out of continuous ones. :) – Puddlebunk Jun 22 '18 at 13:07
  • 1
    It works but with cut() you save yourself a lot of repetition in your code, like the multiple ifelse's and data$c2's. With the 1st argument you refer to your variable, with the 2nd you give the intervals, and with the latter the new categories. – Lennyy Jun 22 '18 at 13:09
10
df <- read.table(text = "
c1  c2  c3
x   2   z
y   5   f
c   3   r
a   11  z
         ", h = T)

df$c4 <- cut(df$c2, c(-Inf,4,9,Inf), c("low", "medium", "high"))

> df

  c1 c2 c3     c4
1  x  2  z    low
2  y  5  f medium
3  c  3  r    low
4  a 11  z   high
Lennyy
  • 5,932
  • 2
  • 10
  • 23