Search code examples
rdataframedplyrtapply

How do I create a new categorical variable from continuous multiple observations?


This is my data:

ID  dist
1   23
1   10
2   12
2   20
3   14
3   33

I want to go through each ID, and create a new column ("state") for the larger value for each ID call it "high" and for the lower value, call it "low".

What's the best way to do this?


Solution

  • Using R base

    > transform(df1, state = ave(dist, ID, FUN= function(x)ifelse(x==max(x), "high", "low")))
    
      ID dist state
    1  1   23  high
    2  1   10   low
    3  2   12   low
    4  2   20  high
    5  3   14   low
    6  3   33  high