Thanks for the quick reply.
One additional question. In case that I have multiple variables that I want to find the maximum values... will the code below work? Or do I need to run it separately?
require(dplyr)
data <- data %>%
group_by(group) %>%
mutate(max_value=max(value), max_weight=max(weight), max_height=max(height))
jlee12.0 - Yes that should work. One thing I always recommend is just trying things yourself. It's good to ask questions when you get stuck, but sometimes I feel like beginners are scared to hit the "run" button because they don't know if their code is right. The best way to find out if your code works is to run it! If it breaks, no harm no foul! I run scripts that error out all the time, many, many times in a row even. That's how I learn!
Also, Mara's code rocks! It does the same thing that your code does, accept it's less typing. By calling across she's applying the max function to the three columns listed in c().
across() is super powerful, and I could've done the same thing in a way that allows for sort of "dynamic" column names, since you might want multiple functions run across several columns (I'll use summarize() here, since it makes more sense). Note, I'm replacing the hard-coded max prefix in the .names argument with {.fn}. {.fn} gets its names from the named list of functions I'll run over the columns we've selected.
data %>%
group_by(group) %>%
summarize(across(c(value, weight, height), list(max = max, min = min), .names = "{.fn}_{.col}"))
There are also lots of ways to select which columns you're running the functions on, since it uses tidyselect syntax.
The colwise dplyr vignette is also really helpful: