Summing and dividing by different categories conversion from Python to R <p>I have a set of vectors containing category values, lets call them, C1, C2,...and I have a frequency vector called Fr. All vectors are of the same length. I want to divide the frequency values in Fr by sums dependent on the categories. In Python using numpy this is fairly easy.</p> <pre><code># Find unique categories unqC1 = np.unique(C1) unqC2 = np.unique(C2) # For each unique category in C1 and C2 sum frequencies and normalize for uC1 in unqC1: for uC2 in unqC2: mask = (uC1 == C1) &amp; (uC2 == C2) nrmFactor = np.sum(Fr[mask]) Fr[mask] /= nrmFactor </code></pre> <p>How can I do this in R? For simplicity lets say I have a table X, in R, with the columns X$Fr, X$C1 and X$C2.</p>