0

I have a df like this:

Id   count
1      0
1      5
1      7
2      5
2      10
3      2
3      5
3      4

and I want to get the maximum count and apply that to the whole "group" based on ID, like this:

Id   count   max_count
1      0        7
1      5        7
1      7        7
2      5        10
2      10       10
3      2        5
3      5        5
3      4        5

I've tried pmax, slice etc. I'm generally having trouble working with data that is in interval-specific form; if you could direct me to tools well-suited to that type of data, would really appreciate it!

Zheyuan Li
  • 71,365
  • 17
  • 180
  • 248
Emily
  • 470
  • 5
  • 16

1 Answers1

1

Figured it out with help from Gavin Simpson here: Aggregate a dataframe on a given column and display another column

maxcount <- aggregate(count ~ Id, data = df, FUN = max)

new_df<-merge(df, maxcount)

Better way:

df$max_count <- with(df, ave(count, Id, FUN = max))
Community
  • 1
  • 1
Emily
  • 470
  • 5
  • 16