I am trying to run an ifelse statement with a by group and cannot see how to do it in R.
For example if I have
ID ORDER
1 1
2 1
3 1
3 2
3 3
6 1
7 1
7 2
I want to create a column which gives a 1 if order = max(order), and a 0 otherwise for each ID. So overall would give
1 1 0 0 1 1 0 1
My ifelse statement is therefore
ifelse(ORDER == max(ORDER), 1, 0)
How would I do this for each ID variable (preferably without a for loop)?
Thanks.