# prepare a sample data set
# 7:21 PM Monday, October 9, 2023
set.seed(1111)
num_pool <- seq_len(10)
id_pool <- c(4226300,
4226046,
4225702,
4223923,
4225365,
4224713,
4223881,
4223464)
foster <- sample(num_pool, 10, replace = TRUE)
aunt <- sample(num_pool, 10, replace = TRUE)
father <- sample(num_pool, 10, replace = TRUE)
mother <- sample(num_pool, 10, replace = TRUE)
id <- sample(id_pool, 10, replace = TRUE)
sample_dataset <-
data.frame(id,
foster,
aunt,
father,
mother) |>
dplyr::mutate(code = "withdrew") |>
dplyr::relocate(code, .before = foster) |>
dplyr::arrange(id)
print(sample_dataset)
#> id code foster aunt father mother
#> 1 4223464 withdrew 10 2 2 10
#> 2 4223464 withdrew 1 4 2 9
#> 3 4223881 withdrew 1 7 10 4
#> 4 4223923 withdrew 10 5 3 7
#> 5 4224713 withdrew 4 2 5 7
#> 6 4225365 withdrew 6 5 1 10
#> 7 4226046 withdrew 6 1 4 4
#> 8 4226300 withdrew 6 6 2 8
#> 9 4226300 withdrew 2 8 6 1
#> 10 4226300 withdrew 7 4 10 8
# merge rows into a single row by ID
sample_dataset |>
dplyr::group_by(id, code) |>
dplyr::summarise_if(is.numeric, sum)
#> # A tibble: 7 × 6
#> # Groups: id [7]
#> id code foster aunt father mother
#> <dbl> <chr> <int> <int> <int> <int>
#> 1 4223464 withdrew 11 6 4 19
#> 2 4223881 withdrew 1 7 10 4
#> 3 4223923 withdrew 10 5 3 7
#> 4 4224713 withdrew 4 2 5 7
#> 5 4225365 withdrew 6 5 1 10
#> 6 4226046 withdrew 6 1 4 4
#> 7 4226300 withdrew 15 18 18 17
Created on 2023-10-09 with reprex v2.0.2
@fletcj3 I am creating a reproducible example here to demonstrate my solution to your question. I hope you find this helpful. If have any further questions, please let me know.