One more alternative using the lesser known chron package along with dplyr. Personally, I think this is more concise and easier to read and interpret.
library(dplyr)
df <- data.frame(
id = c(1L, 2L, 3L, 4L, 5L, 6L, 7L),
time = c("07:45:00", "09:45:00", NA, "08:45:00", "23:50:00", "07:45:00", "16:45:00")
)
df %>%
mutate(
time = chron::times(time),
time = if_else(is.na(time), mean(time), time)
)
#> id time
#> 1 1 07:45:00
#> 2 2 09:45:00
#> 3 3 12:25:50
#> 4 4 08:45:00
#> 5 5 23:50:00
#> 6 6 07:45:00
#> 7 7 16:45:00