简体   繁体   中英

Fill a column based on max values by condition in R

I need to fill a new column based on the max values per group.

So I have

A B  C
1 1  0
1 9  0
2 5  0
2 10 0
2 15 0
3 1  0 
3 2  0
4 5  0
4 6  0

I need to fill $C with 1 for each maximum value in $B per grouping of $A

So:

A B  C
1 1  0
1 9  1
2 5  0
2 10 0
2 15 1
3 1  0 
3 2  1
4 5  0
4 6  1

Appreciate the help

We can use base R ave to match maximum value in each group

df$C <- +(with(df, B == ave(B, A, FUN = max)))

df
#  A  B C
#1 1  1 0
#2 1  9 1
#3 2  5 0
#4 2 10 0
#5 2 15 1
#6 3  1 0
#7 3  2 1
#8 4  5 0
#9 4  6 1

The same in dplyr would be

library(dplyr)

df %>%
  group_by(A) %>%
  mutate(C = +(B == max(B)))

We can also match it with index of maximum value

df$C <- with(df, ave(B, A, FUN = function(x) seq_along(x) == which.max(x)))

and

df %>%
  group_by(A) %>%
  mutate(C = +(row_number() == which.max(B)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM