I have a huge data frame with several missing value that I need to replace as follow:
| Cycle A | Cycle B | Cycle C | ..... |
|---|---|---|---|
| na | na | na | |
| na | na | na | |
| na | na | na | |
| -1 | na | 0 | |
| -1 | -2 | 0 | |
| na | -2 | na | |
| na | na | na | |
| na | na | 1 | |
| 0 | -1 | 1 | |
| 0 | -1 | na | |
| na | na | na | |
| na | na | na | |
| na | 0 | 2 | |
| 1 | 0 | 2 | |
| 1 | na | na | |
| na | na | na |
For each column I need to replace the NA's by the next number that appears, to have something like that:
| Cycle A | Cycle B | Cycle C | ..... |
|---|---|---|---|
| -1 | -2 | 0 | |
| -1 | -2 | 0 | |
| -1 | -2 | 0 | |
| -1 | -2 | 0 | |
| -1 | -2 | 0 | |
| 0 | -2 | 1 | |
| 0 | -1 | 1 | |
| 0 | -1 | 1 | |
| 0 | -1 | 1 | |
| 0 | -1 | 2 | |
| 1 | 0 | 2 | |
| 1 | 0 | 2 | |
| 1 | 0 | 2 | |
| 1 | 0 | 2 | |
| 1 | 1 | 3 | |
| 2 | 1 | 3 |
Any idea how to do that? Thank you.
tidyr::fill(df, starts_with("Cycle"), .direction = "up")