There will be tons of ways to do this. The library zoo
has a built in function na.aggregate
which does precisely what you want.
library(zoo)
d <- data.frame(key = rep(c("key1", "key2"), each = 3),
value = c(10, 12, NA, 2, 3, NA))
with(d, na.aggregate(value, by = key))