generate column values with multiple conditions in R

后端 未结 8 1998
甜味超标
甜味超标 2020-12-29 00:22

I have a dataframe z and I want to create the new column based on the values of two old columns of z. Following is the process:

&g         


        
8条回答
  •  醉梦人生
    2020-12-29 00:35

    Based on the suggestion of Señor :

    > z$q <- ifelse(z$x == 2, z$t * 2,
             ifelse(z$x == 4, z$t * 4,
             ifelse(z$x == 7, z$t * 3,
                              z$t * 1)))
    > z
        x  y  t  q
    1   1 11 21 21
    2   2 12 22 44
    3   3 13 23 23
    4   4 14 24 96
    5   5 15 25 25
    6   6 16 26 26
    7   7 17 27 81
    8   8 18 28 28
    9   9 19 29 29
    10 10 20 30 30
    

提交回复
热议问题