в Base-R,
merge(senior, aggregate(hours_care ~ id_senior, relative, sum), by = "id_senior", all.x=T)
id_senior hours_care
1 1 9
2 2 NA
3 3 6
4 4 3
5 5 NA
...
20 20 NA
Изменить:
, чтобы получить дополнительный столбец в
merge(senior, merge(aggregate(id_relative ~ id_senior, relative, length),aggregate(hours_care ~ id_senior, relative, sum)), by = "id_senior", all.x=T)
id_senior id_relative hours_care
1 1 2 9
2 2 NA NA
3 3 3 6
4 4 1 3
5 5 NA NA
6 6 NA NA
...
Данные:
relative <- structure(list(id_senior = c(1L, 1L, 3L, 3L, 3L, 4L), id_relative = c(1L,
2L, 1L, 2L, 3L, 1L), hours_care = c(3L, 6L, 5L, 0L, 1L, 3L)), class = "data.frame", row.names = c(NA,
-6L))
senior <- data.frame(id_senior = 1:20)