lecb
lecb

Reputation: 409

Select max value across group - R/Dplyr solution

I'm hoping this will be easy to answer. I have some data, and I want to find the max value across a group, however I'm having issue with my output.

Dummy data:

ID  value
1     10
1     2
1     3
1     4
2     1
2     2
2     3
3     4
3     5
3     6

I want to find the max across the groups, however I want the max value to be on the first line of the record i.e. my output should look like:

ID  value   max_value
1      10         10
1      2    
1      3    
1      4    
2      1          3
2      2    
2      3    
3      4          6
3      5    
3      6    

I've managed to get the max value per record/ID, but I'm having issue with making it the first line of each ID, as I'd like to retain all lines in the database.

library(dplyr)
df <- df %>%
group_by(ID) %>%
filter(value == max(value)) %>%

Hopefully it's just something stupid I'm doing! Thanks in advance,

Upvotes: 0

Views: 199

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389235

You can assign max(value) for each ID in the 1st row.

library(dplyr)

df %>%
  group_by(ID) %>%
  mutate(max_value = ifelse(row_number() == 1, max(value), NA_integer_)) -> result

result

#      ID value max_value
#   <int> <int>     <int>
# 1     1    10        10
# 2     1     2        NA
# 3     1     3        NA
# 4     1     4        NA
# 5     2     1         3
# 6     2     2        NA
# 7     2     3        NA
# 8     3     4         6
# 9     3     5        NA
#10     3     6        NA

Upvotes: 1

Related Questions