I have a dataset that contains features Date
, Age
and Customer_ID
. Some of the rows in Age
have missing values (NAs) in them and I would like to impute them.
Here is some example data:
Date <- c("201101", "201102", "201101", "201102", "201103")
Age <- c("12-17", "12-17", "30-35", NA, NA)
Customer_ID <- c("1234", "1234", "5678", "5678", "5678")
df <- data.frame(Date, Age, Customer_ID)
Date Age Customer_ID
201101 12-17 1234
201102 12-17 1234
201101 30-35 5678
201102 NA 5678
201103 NA 5678
I would like to replace the NAs in Age
with 30-35.
So for all NAs, it has to check whether there is another row with the same Customer_ID
and replace the NA with the value for Age
stated in the other row.
Any ideas on how to do this? Thanks.