How do I subset missing values in one dataframe with values from another?
Let's say I have two datasets:
dataset 1 shows the amount of food that is produced by a country each day.
country day tonnes of food
## 1 china 1 6
## 2 china 1 NA
## 3 china 2 2
## 4 china 2 NA
dataset2 is the average amount of food by day
country day average tonnes of food
## 1 china 1 6
## 3 china 2 2
How can I fill in the NAs of dataset1 with the averages from dataset2.
I.e. IF is.na(dataset1$tonnes)
is TRUE then fill in with average for day from dataset2$averagetonnes