Похоже, вам просто нужно объединить и объединить ваши данные.
Вот несколько примеров данных
patient_df <- structure(list(patient_id = 1:3, gender = structure(c(2L, 1L,
2L), .Label = c("F", "M"), class = "factor")), class = "data.frame", row.names = c(NA,
-3L))
print(patient_df)
## patient_id gender
## 1 1 M
## 2 2 F
## 3 3 M
procedure_df <- structure(list(procedure_id = c(1, 2, 3, 1, 2, 1), patient_id = c(1,
1, 1, 2, 2, 3), cost = c(10, 5, 12, 10, 5, 10)), class = "data.frame", row.names = c(NA,
-6L))
print(procedure_df)
## procedure_id patient_id cost
## 1 1 1 10
## 2 2 1 5
## 3 3 1 12
## 4 1 2 10
## 5 2 2 5
## 6 1 3 10
Давайте объединим данные процедуры
library(dplyr)
total_costs <- procedure_df %>%
group_by(patient_id) %>%
summarize(total_cost = sum(cost)) %>%
ungroup()
print(total_costs)
## # A tibble: 3 x 2
## patient_id total_cost
## <dbl> <dbl>
## 1 1 27
## 2 2 15
## 3 3 10
И затем объединить его с данными пациента
patient_costs <- left_join(patient_df, total_costs, by = "patient_id")
print(patient_costs)
## patient_id gender total_cost
## 1 1 M 27
## 2 2 F 15
## 3 3 M 10