-4

I have created Dummy_Value field in R for below table. I have to print maximum (Value) by ID in Dummy_Value.. How to do in R

ID      Value   Dummy_Value
1        20
5        15
8        16
6        8
7        65
8        40
5        25 
1        62
6        14
7        20
9        11
8        12
9        36
1        26
4        13
ArunK
  • 1,731
  • 16
  • 35
Praveen Kumar
  • 107
  • 1
  • 7

1 Answers1

1

Use ave():

df$Dummy_Value <- ave(df$Value,df$ID,FUN=max);
df;
##    ID Value Dummy_Value
## 1   1    20          62
## 2   5    15          25
## 3   8    16          40
## 4   6     8          14
## 5   7    65          65
## 6   8    40          40
## 7   5    25          25
## 8   1    62          62
## 9   6    14          14
## 10  7    20          65
## 11  9    11          36
## 12  8    12          40
## 13  9    36          36
## 14  1    26          62
## 15  4    13          13

Data

df <- data.frame(ID=c(1L,5L,8L,6L,7L,8L,5L,1L,6L,7L,9L,8L,9L,1L,4L),Value=c(20L,15L,16L,8L,
65L,40L,25L,62L,14L,20L,11L,12L,36L,26L,13L));
bgoldst
  • 34,190
  • 6
  • 38
  • 64