The "classic" R command is tapply
n <- 17; fac <- factor(rep(1:3, length = n), levels = 1:5)
df=data.frame(target=1:n, factor=fac)
with(df, tapply(target, fac, sum))
by and aggregate both work but the input output as lists or by objects is daft. Also ddply from plyr will work but is somewhat less clear syntactically although it comes into its own for more complicated examples.