consider the following data.frame:
df1 <- data.frame(index = c(1,1,1,2,2,2),
country = c("AR","AR", "DE", "CH", "AR", "DE"),
type = c("CoL", "CPI", "GDP", "CPI", "CPI", "GDP"),
value = c(11.12, 53.32, 10.42, 120.23, NA, NA))
> df1
index country type value
1 1 AR CoL 11.12
2 1 AR CPI 53.32
3 1 DE GDP 10.42
4 2 CH CPI 120.23
5 2 AR CPI NA
6 2 DE GDP NA
How can I replace NAs by the corresponding value of previous index, but only if all other attributes except for index match each other, so the result would look like this:
> df2
index country type value
1 1 AR CoL 11.12
2 1 AR CPI 53.32
3 1 DE GDP 10.42
4 2 CH CPI 120.23
5 2 AR CPI 53.32
6 2 DE GDP 10.42
Since the actual data.frames are quite big I intend to avoid using for loops to screen the data multiple times.
Thanks folks!