How can I use one column to determine where I get the value for another column?

前端 未结 3 2013
伪装坚强ぢ
伪装坚强ぢ 2021-01-25 15:33

I\'m trying to use one column to determine which column to use as the value for another column It looks something like this:

     X   Y  Z   Target
1    a   b           


        
3条回答
  •  攒了一身酷
    2021-01-25 16:11

    You could try apply rowwise like this:

    transform(df, TargetValue = apply(df, 1, function(x) x[x["Target"]]))
    #   X Y Z Target TargetValue
    # 1 a b c      X           a
    # 2 d e f      Y           e
    # 3 g h i      Z           i
    

提交回复
热议问题