I'm trying to find a way to Replace NAs for a group of values with a non-NA character by group, if this non-NA character does not always appear in the same place (first row or other). The solutions I found don't work for characters or only fill based on a previous or subsequent value.
Here is an example of data:
participant_id <- c("ps1", "ps1", "ps1", "ps1", "ps2", "ps2", "ps3", "ps3", "ps3", "ps3")
test <- c("test1", NA, NA, NA, NA, "test2", NA, NA, "test3", NA)
data.frame(participant_id, test)
This is what I would like to end up with:
participant_id | test |
---|---|
ps1 | test1 |
ps1 | test1 |
ps1 | test1 |
ps1 | test1 |
ps2 | test2 |
ps2 | test2 |
ps3 | test3 |
ps3 | test3 |
ps3 | test3 |
ps3 | test3 |