library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
# Example data frame with multiple rows per id
df <- data.frame(
id = c(1, 1, 2, 2, 3),
values = c(10, 20, 30, 40, 50)
)
df
## id values
## 1 1 10
## 2 1 20
## 3 2 30
## 4 2 40
## 5 3 50
# Calculate the sum of values for each id
sum_by_id <- df %>%
group_by(id) %>%
summarise(sum_of_values = sum(values))
sum_by_id
## # A tibble: 3 × 2
## id sum_of_values
## <dbl> <dbl>
## 1 1 30
## 2 2 70
## 3 3 50