how to find a max for y value at each level of z value

Hello im curently working on a project and i did find myself blocked by a simple thing, i have a datset (shown below ) image

i need to find the max value of Value 6 for each "level" of let's say value 1 .
im sure it's a really simple thing to do but im currently stuck on this and can't bring my brain to see the solution.
(of course for my dataset is longer than this )
TY very much

Hi @Moty,

There are a few ways of doing this but I think the most clear way is using some functions from the dplyr package. Try this:


data %>%
  group_by(value1) %>%
  summarise(max_value6 = max(value6, na.rm = TRUE)) 

This will return a data set with the maximum of value6 for each group defined by the values from value1.

dt[, .(max_6 = max(value_6, na.rm=TRUE), by=.(value_1)]

@mattwarkentin Ty so much for your quick answer , i would really prefer a way to do it by making a function without any additional package , it's for an university project and it's one of the last thing i need to do for it , i would have used that kind of thing if it was for an actual work or myself but i need to do it the "old and painful way".

Okay, here is one way using only base R functions:

aggregate(value6 ~ value1, data = data, max)

im very thankfull for your quick answer , i did it in another way , after throwing my brain at it for some time (and giving my brain way to much coffe )
i did it this way
minup<-sapply(levels(ds$value1), function(level){min(ds$value6[ds$value1==level])})
what do you think of it
(minup being just the name of the variable i was trying to get and ds being my dataframe/matrix.)

If it works, it works. There are lots of ways to achieve the same results in R. I do notice that in your question you wanted the max value, but in your code you use min. Otherwise, your code does the job.

The benefit of using aggregate is that it extends easily to groups defined by multiple dimensions/variables, rather than just one, and it harnesses the power of formulas so the code is less cluttered with repeating the name of the data set and variables.

For example, if you wanted to find the max for value6 across groups defined by multiple dimensions, this is easily done with aggregate:

aggregate(value6 ~ value1 + value2, data = ds, max)

yea i guess i still need to learn how to use the full power of the aggregate , but yea i wanted the max value because i basiclly want both ^^ , TY verymuch for that .
i still need to learn a lot and even if i did not end up using your solution i now know that it's a way of doing it . thanks again. i will close this thrad since i have my answer .
my best wish to you and everyone who helped.
edit: i don't seems to find how to close the thread so i guess it will remain open till it does it by itself or a mod close it .

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

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