1

I have single observations for some and double observations for others. In the case of double observations in x, I want to choose the row with the lower y value to remove that row.

ex <- data.frame('x'= c(1:5, 1:3,5:6), 'y'= c(70,73,72,49,60,14,50,46,13,29))

Original df: enter image description here

Desired result: enter image description here

W Hampton
  • 31
  • 7

1 Answers1

1

You can use aggregate to find the largest number per group:

aggregate(y ~ x, ex, max)
#  x  y
#1 1 70
#2 2 73
#3 3 72
#4 4 49
#5 5 60
#6 6 29
GKi
  • 27,870
  • 2
  • 18
  • 35