I have a data frame as following:
id weight value
231 50 0.6
231 50 0.43
420 30 0.86
420 30 0.12
How can I multiply all values per each id and weight to have the following table:
id weight value
231 50 0.6*0.43
420 30 0.86*0.12
Use prod()
to multiply all the values in its arguments.
library(dplyr)
df %>%
group_by(id, weight) %>%
summarise(prod = prod(value))
# # A tibble: 2 x 3
# # Groups: id [2]
# id weight prod
# <int> <int> <dbl>
# 1 231 50 0.258
# 2 420 30 0.103
Or a base way
aggregate(value ~ id + weight, df, prod)