Looking through the ave function, I found a wonderful line:
split(x, g) <- lapply(split(x, g), FUN)
Interestingly, this line changes the value of x , which turned out to be unexpected. I was expecting split(x,g) result in a list that could be assigned, but then dropped later. My question is: why does the value of x change?
Another example might explain better:
a <- data.frame(id=c(1,1,2,2), value=c(4,5,7,6))
Not only a was changed, but it changed to a value that does not look like the right side of the job! Even if assigning split(a,a$id) somehow changes a (which I don't understand), why does this lead to data.frame instead of list ?
Please note that I understand that there are better ways to accomplish this task. My question is: why does split(a,a$id)<-lapply(split(a,a$id),function(x) x[which.max(x$value),]) change a ?
source share