Assuming that dataframe is named 'dat' then aggregate.formula
which is one of the generics of aggregate:
> aggregate( Z ~ X + Y, data=dat, FUN=sum)
X Y Z
1 1 1 2435
2 2 1 534
3 1 2 91
4 2 2 97
5 1 3 1924
6 2 3 161
7 1 4 582
8 2 4 122
9 2 5 403
Could also have used xtabs
which returns a table object and then turn it into a dataframe with as.data.frame:
as.data.frame( xtabs( Z ~ X+Y, data=dat) )