I need to use na.locf
from the zoo
package to replace NA values with the last observed value. However, I need to do this only for specific country & variable pairs. These pairs are specified logically using a seperate data frame, an example of which is shown below.
Country <- c("FRA", "DEU", "CHE")
acctm <- c(0, 0, 1)
acctf <- c(1, 1, 0)
df1 <- data.frame(Country, acctm, acctf)
Country acctm acctf
1 FRA 0 1
2 DEU 0 1
3 CHE 1 0
a 1
meaning use na.locf
for this pair. An example of the dataset where replacement would be needed is shown below.
Country <- c("FRA", "FRA", "DEU", "DEU", "CHE", "CHE")
Year <- c(2010, 2020, 2010, 2020, 2010, 2020)
acctm <- c(20, 30, 10, NA, 20, NA)
acctf <- c(20, NA, 15, NA, 40, NA)
df2 <- data.frame(Country, Year, acctm, acctf)
Country Year acctm acctf
1 FRA 2010 20 20
2 FRA 2020 30 NA
3 DEU 2010 10 15
4 DEU 2020 NA NA
5 CHE 2010 20 40
6 CHE 2020 NA NA
Given both of the example datasets, the result of the function executing na.locf on df2 for country/variable pairs indicated by df1
should look like this:
acctm <- c(20, 30, 10, NA, 20, 20)
acctf <- c(20, 20, 15, 15, 40, NA)
df3 <- data.frame(Country, Year, acctm, acctf)
Country2 Year acctm acctf
1 FRA 2010 20 20
2 FRA 2020 30 20
3 DEU 2010 10 15
4 DEU 2020 NA 15
5 CHE 2010 20 40
6 CHE 2020 20 NA
The real application is a much larger dataset, so "calls" should be generalized. Thanks.