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 (see sample code below). STATA
- y refers to the date value of 20191125
local y 20191125
foreach v in attend child sibling{
replace `v'=`v'`y' if !missing(`v'`y')
}
Data
+----+----+---+-----------+-----------+-----------+
|attend | child | sibling | attend20191125 | child20191125 | sibling20191125 |
+----+----+---+-----------+-----------+-----------+
| 1 | 2 | 3 | 6 | 8 | 0 |
| 1 | NA | 0 | 1 | 1 | 1 |
| NA | 0 | 1 | 5 | 4 | 2 |
+----+----+---+-----------+-----------+-----------+
Potential output:
+----+----+---+-----------+-----------+-----------+
|attend | child | sibling | attend20191125 | child20191125 | sibling20191125 |
+----+----+---+-----------+-----------+-----------+
| 1 | 2 | 3 | 6 | 8 | 0 |
| 1 |1 | 0 | 1 | 1 | 1 |
| 5 | 0 | 1 | 5 | 4 | 2 |
+----+----+---+-----------+-----------+-----------+
I know how to replace NA values for one column from another column. How can I do the same using purrr dynamically? How can I tell R to replace values from a variable with the same name + date prefix? I have about 25 variables in my dataset that require this.
#Method 1: I can do it one variable at a time
df%<>%
mutate(attend=ifelse(is.na(attend)==T, attend20191125, attend),
child=ifelse(is.na(child)==T, child20191125, child),
sibling=ifelse(is.na(sibling)==T, sibling20191125, sibling))
#Method 2: using mutate_at (but not sure how I can dynamically refer to the date indexed variable?).
df%<>%
mutate_at(c("attend", "child", "sibling"), .=ifelse(is.na(.)==T, var20191125, .))