verify complex group by /summarize

hi I want to to ensure this code I made below is correct. I have my data. I want to group by "id" and "year" summarize and make a new column in which I take the value of the categorical variable "cat" within the grouping WHERE I see the max value of the variable "age". if there is two "cats" with the same age, take the earliest one by arranging id and year.
is the below code correct in achieving my desired goal?

data %>%
group_by(id, year) %>%
arrange(id, year) %>% 
summarize(max_cat = cat[which.max(age)])

thank you,

I also welcome any alternative that is better Thanks

Could you supply us with some sample data?

A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need. Just do dput(mydata) where mydata is your data. Copy the output and paste it here.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.