This question already has an answer here:
- Replacing NAs with latest non-NA value 15 answers
I am trying to replace update values for x, q, z using the recently available version of the variable indexed by date. In STATA, we can easily do it as a for loop. How can I do the same using purrr dynamically?
STATA
- y refers to the date value of 20191125
local y 20191125
foreach v in x, q, z{
replace `v'=`v'`y' if !missing(`v'`y')
}
Data
+----+----+---+-----------+-----------+-----------+
| X | Q | Z | X20191125 | Q20191125 | Z20191125 |
+----+----+---+-----------+-----------+-----------+
| 1 | 2 | 3 | 6 | 8 | 0 |
| 1 | NA | 0 | 1 | 1 | 1 |
| NA | 0 | 1 | 5 | 4 | 2 |
+----+----+---+-----------+-----------+-----------+
Aucun commentaire:
Enregistrer un commentaire