0

I have a df that has 2 columns - game_id and score

game id   score
1           55
1           59 
1           62
1           71
2           74  
2           65
2           89
2           98

I would want the result to be

game id     score
1            55
2            74

Just trying to grab the first row for each game id

a for and if loop to delete

Quinten
  • 35,235
  • 5
  • 20
  • 53

2 Answers2

1

You can use the function first to filter on the group-first row:

library(dplyr)
df %>%
  group_by(game_id) %>%
  filter(score == first(score))

Data:

df <- data.frame(
  game_id  = c(1,1,1,1,2,2,2,2),
  score = c(55,59,62,71,74,65,89,98)
)
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34
0

A base R approach using aggregate

aggregate(. ~ `game id`, df, "[", 1)
  game id score
1       1    55
2       2    74

Data

df <- structure(list(`game id` = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), 
    score = c(55L, 59L, 62L, 71L, 74L, 65L, 89L, 98L)), 
class = "data.frame", row.names = c(NA, 
-8L))
Andre Wildberg
  • 12,344
  • 3
  • 12
  • 29