In the following example, I got this data:
x = c("a", "a", "a", "a", "b", "b")
y = c("s", "s", "t", "t", "s", "s")
z = c(1, 2, 3, 4, 5, 6)
data = data.frame(cbind(x, y, z))
Then I need to sum up z
by x
and y
, then put the sum as w
like below. Note that I don't want to merge rows and the results should still have the same number of rows. How can this be achieved, please? Thank you.
x y z w
a s 1 3
a s 2 3
a t 3 7
a t 4 7
b s 5 5
b t 6 6