【发布时间】:2021-12-12 22:44:01
【问题描述】:
如果此非 NA 字符并不总是出现在同一位置(第一行或其他),我正在尝试找到一种方法来逐组替换具有非 NA 字符的一组值的 NA。我发现的解决方案不适用于字符或仅基于先前或后续值填充。
这是一个数据示例:
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)
这就是我想要的结果:
| participant_id | test |
|---|---|
| ps1 | test1 |
| ps1 | test1 |
| ps1 | test1 |
| ps1 | test1 |
| ps2 | test2 |
| ps2 | test2 |
| ps3 | test3 |
| ps3 | test3 |
| ps3 | test3 |
| ps3 | test3 |
【问题讨论】: