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


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.