我正在尝试获取id的唯一值y的最大值。
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
任何帮助将不胜感激!
我们可以使用group_by
'id'
library(dplyr)
df %>%
type.convert(as.is = TRUE) %>%
group_by(id) %>%
mutate(output = max(y))
或使用ave
中的base R
df <- type.convert(df, as.is = TRUE)
df$output <- with(df, ave(y, id, FUN = max))