This post is based on this question: Running ifelse statement by a variable
Given some data
  col1 col2
1    B   10
2    B    7
3    C    7
4    D    1
5    A    3
6    D    2
to which we want to add a new column that indicates whether the value of 'col2' is the maximum of 'col2' grouped by 'col1'.
In base R one can use transform and ave
transform(df, new_col = ave(col2, col1, FUN = function(x) x == max(x)))
  col1 col2 new_col
1    B   10       1
2    B    7       0
3    C    7       1
4    D    1       0
5    A    3       1
6    D    2       1
My question is why 'new_col' is of type integer and not logical?
@RichScriven pointed out in the comments that that's because 'col2' is already numeric but I don't understand that.
Thanks!
data
df <- structure(list(col1 = c("B", "B", "C", "D", "A", "D"), col2 = c(10L, 
7L, 7L, 1L, 3L, 2L)), .Names = c("col1", "col2"), row.names = c(NA, 
-6L), class = "data.frame")
 
    