Fill the missing values using the last observation carried forward for each id

49 Views Asked by At

Using the code below, I am trying to use the baseline values of the marital column and carry the value forward for each id. The steps below are working except for the last chunk of the code which is unable to carry forward the baseline values.

Identify the baseline values for each 'id'

baseline_values <- tapply(female_98_2020$year, female_98_2020$hhidpn, min)

Create a new variable 'marital_base' with baseline values

female_98_2020$marital_base <- ifelse(female_98_2020$year == baseline_values[as.character(female_98_2020$hhidpn)], female_98_2020$marital, NA)

Fill the missing values using the last observation carried forward (locf)

library(zoo) 
female_98_2020 <- female_98_2020 %>%
  group_by(hhidpn) %>%
  mutate(marital_base = zoo::na.locf(marital_base, na.rm = FALSE)) %>%
  ungroup()
1

There are 1 best solutions below

1
Nader Mehri On

I have found out that na.rm has to be TRUE:

library(zoo) 
female_98_2020 <- female_98_2020 %>%
  group_by(hhidpn) %>%
  mutate(marital_base = zoo::na.locf(marital_base, na.rm = TRUE)) %>%
  ungroup()