Question:
How to apply functions to a subset of the data in a vectorized manner.
Example:
For the data frame below:
x=c(1,2,1,2,1,2)
y=c(3,4,5,4,3,2)
df=data.frame(x,y)
I would like to apply a function (i.e. min()) to all y values for each of the x value, and collect it in a vector.
Basically, I would like to have a vectorized version of this:
nb = max(x);
V = rep(0.0, nb)
for(i in 1:nb){
v = df [ x == i, ]$y;
V[i] <- min(v);
}
# basically here:
# V[1] = min( df$y for x=1)
# V[2] = min( df$y for x=2)