sumproduct per grouped value in R

I have a data frame and I have grouped my data based on one of the columns (class). I want to create a sumproduct of two other columns (a and b) for each one of the unique values in the column that I have grouped them against. I have the following code but it doesn't calculate it correctly.

pools <- pools %>%
arrange(class) %>%
group_by(class) %>%

I think you may be needing to summarise rather than mutate ?

iris %>% group_by(Species) %>%

I want to keep all my rows in the new dataframe so summarize would not allow me that since I have many data with the same class so they have to have the same sumproduct as well.

ok, fair enough. Can you say more about why you believe the calculation to be wrong.
changing my example from summarise to mutate, gives the same values but repeated over rows in the group, it all seems correct to me.

Yes thank you after all I get the correct one. Would you know by any chance how to do the same but using three columns this time? The function crossprod can only be used with two so I am looking how to take the sumproduct of three columns now (but again I want to keep all values)

sum product of 3 columns

iris %>% group_by(Species) %>%

Thank you very much!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.