I'm trying to get the max value of y per unique value of id.
id <- c("1", "1", "1", "2", "2", "2")
y <- c("2.43", "2.11", "2.31", "3.11", "2.12", "2.10")
output <- c("2.43", "2.43", "2.43", "3.11", "3.11", "3.11")
df <- data.frame(id, y, output)
id y output
1 1 2.43 2.43
2 1 2.11 2.43
3 1 2.31 2.43
4 2 3.11 3.11
5 2 2.12 3.11
6 2 2.10 3.11
Any help would be much appreciated!!
We can use group_by
'id'
library(dplyr)
df %>%
type.convert(as.is = TRUE) %>%
group_by(id) %>%
mutate(output = max(y))
Or with ave
from base R
df <- type.convert(df, as.is = TRUE)
df$output <- with(df, ave(y, id, FUN = max))