Rather than the current 0 fill for NAs, Is there anything in pivot_wider where it can replace it with the last known values from the same column?
library('tidyr')
library('dplyr')
fish_encounters %>%
group_by(station) %>%
mutate(seen = cumsum(seen)) %>%
pivot_wider(names_from = station, values_from = seen,values_fill = list(seen = 0))
# A tibble: 19 x 12
fish Release I80_1 Lisbon Rstr Base_TD BCE BCW BCE2 BCW2 MAE MAW
<fct> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
1 4842 1 1 1 1 1 1 1 1 1 1 1
2 4843 2 2 2 2 2 2 2 2 2 2 2
3 4844 3 3 3 3 3 3 3 3 3 3 3
4 4845 4 4 4 4 4 0 0 0 0 0 0
5 4847 5 5 5 0 0 0 0 0 0 0 0
6 4848 6 6 6 5 0 0 0 0 0 0 0
7 4849 7 7 0 0 0 0 0 0 0 0 0
8 4850 8 8 0 6 5 4 4 0 0 0 0
9 4851 9 9 0 0 0 0 0 0 0 0 0
10 4854 10 10 0 0 0 0 0 0 0 0 0
11 4855 11 11 7 7 6 0 0 0 0 0 0
12 4857 12 12 8 8 7 5 5 4 4 0 0
13 4858 13 13 9 9 8 6 6 5 5 4 4
14 4859 14 14 10 10 9 0 0 0 0 0 0
15 4861 15 15 11 11 10 7 7 6 6 5 5
16 4862 16 16 12 12 11 8 8 7 7 0 0
17 4863 17 17 0 0 0 0 0 0 0 0 0
18 4864 18 18 0 0 0 0 0 0 0 0 0
19 4865 19 19 13 0 0 0 0 0 0 0 0
I understand this can be achieved with the code below, but i'd like to do this in multidplyr environment which has yet to support mutate_at or summarise_at functions.
fish_encounters %>%
pivot_wider(names_from = station, values_from = seen,values_fill = list(seen = 0)) %>%
mutate_at(.vars = vars(-fish),cumsum)