How to create a new variable in a data.frame based on a condition?

前端 未结 2 1625
梦谈多话
梦谈多话 2020-12-24 14:51

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

2条回答
  •  有刺的猬
    2020-12-24 15:35

    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.

提交回复
热议问题