Replace NA when last and next non-NA values are equal
问题 I have a sample table with some but not all NA values that need to be replaced. > dat id message index 1 1 <NA> 1 2 1 foo 2 3 1 foo 3 4 1 <NA> 4 5 1 foo 5 6 1 <NA> 6 7 2 <NA> 1 8 2 baz 2 9 2 <NA> 3 10 2 baz 4 11 2 baz 5 12 2 baz 6 13 3 bar 1 14 3 <NA> 2 15 3 <NA> 3 16 3 bar 4 17 3 <NA> 5 18 3 bar 6 19 3 <NA> 7 20 3 qux 8 My objective is to replace the NA values that are surrounded by the same "message" using the first appearance of the message (the least index value) and the last appearance