R语言 删除年龄较高/较低的任何值占主导地位的行

x8diyxa7  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(133)

我有一个有两列的图表,年龄和性能,每个年龄有一个观察结果。我想删除性能值低于年龄较高的任何性能的所有行。但只有当年龄高于最大性能的年龄时。如何做到这一点?
示例 Dataframe :

structure(list(age = c(31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 
57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 
73, 74), performance = c(3.2341087962963, 3.13613425925926, 3.1553125, 
3.22259259259259, 3.11091435185185, 3.41855324074074, 3.21152777777778, 
3.12123842592593, 3.49372685185185, 3.69923611111111, 3.51280092592593, 
3.30284722222222, 3.09113425925926, 3.01299768518518, 3.05696759259259, 
3.3383912037037, 3.1884837962963, 3.00399305555556, 3.01096064814815, 
3.0466087962963, 3.06674768518519, 3.05023148148148, 3.0993287037037, 
2.84232638888889, 2.90362268518519, 2.82956018518519, 3.02834490740741, 
2.74263888888889, 2.87833333333333, 2.56236111111111, 2.73252314814815, 
2.48283564814815, 2.7869212962963, 2.50503472222222, 2.46137731481481, 
2.4915162037037, 2.4375, 2.12164351851852, 2.413125, 2.38354166666667, 
2.08234953703704, 1.77328703703704, 1.87770833333333, 2.00084490740741
)), row.names = c(NA, -44L), class = "data.frame")

为了使它更复杂,我想做相反的最大性能。也就是说,当年龄低于最大性能的年龄时,我想删除性能低于年龄较低的任何性能的行。
这是散点图在所有值下的外观(在归一化尺度上)。

vxqlmq5t

vxqlmq5t1#

我相信还有更简洁的方法,但这是我的第一个想法:

library(dplyr); library(ggplot2)
df %>%
  mutate(highest_yet = cummax(performance)) %>%
  arrange(-age) %>%
  mutate(highest_remaining = cummax(performance)) %>%
  filter(if_else(highest_yet < max(performance),
                 performance >= highest_yet,
                 performance >= highest_remaining)) %>%
  ggplot(aes(age, performance)) + geom_point()

相关问题