For each group, defined by 'id', I would like to select the value in the first row of columns 'x' and 'y', and replace all subsequent values with that first value.
Some data:
id Visit x y
1 1 0 1
1 2 1 2
1 3 2 8
2 9 1 11
2 10 12 14
I want:
id Visit x y
1 1 0 1
1 2 0 1 # <- x & y replaced with first values of 'id' 1
1 3 0 1 #
2 9 1 11
2 10 1 11 # <- x & y replaced with first values of 'id' 2
I tried this:
df1 <- df %>%
arrange(id, Visit) %>%
group_by(id) %>%
fill(x, y,
.direction = 'down',)
However, that doesn't seem to be doing it. Can someone help?