EDIT: I added another column with day in the sample data, as beep is nested within day.
I have the following data. I am trying to get a sum of a in the row of beep = 3 by, beep, day, and id. See column b for what I would like to achieve. I have tried it using dyplr and group_by, but so far no succes. Any ideas on how to approach this are much appreciated!
Note: actual data contains missings in beep (skipped)
structure(list(id = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L), date = c("1-1-2022", "1-1-2022", "1-1-2022", "2-1-2022",
"2-1-2022", "2-1-2022", "1-1-2022", "1-1-2022", "1-1-2022", "2-1-2022",
"2-1-2022", "2-1-2022"), beep = c(1L, 2L, 3L, 1L, 2L, 3L, 1L,
2L, 3L, 1L, 2L, 3L), a = c(1L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L,
1L, 1L, 0L), b = c(NA, NA, 2L, NA, NA, 1L, NA, NA, 1L, NA, NA,
2L)), class = "data.frame", row.names = c(NA, -12L))
| id | day | beep | a | b |
|---|---|---|---|---|
| 1 | 1-1-22 | 1 | 1 | NA |
| 1 | 1-1-22 | 2 | 0 | NA |
| 1 | 1-1-22 | 3 | 1 | 2 |
| 1 | 2-1-22 | 1 | 1 | NA |
| 1 | 2-1-22 | 2 | 0 | NA |
| 1 | 2-1-22 | 3 | 0 | 1 |
| 2 | 1-1-22 | 1 | 1 | NA |
| 2 | 1-1-22 | 2 | 0 | NA |
| 2 | 1-1-22 | 3 | 0 | 1 |
| 2 | 2-1-22 | 1 | 1 | NA |
| 2 | 2-1-22 | 2 | 1 | NA |
| 2 | 2-1-22 | 3 | 0 | 2 |