I have a data set of individuals with differing numbers of repeated observations and a value that is sometimes only filled in the final observation, say:
id <- c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3)
order <- c(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4)
value <- c(NA, NA, NA, 3, NA, NA, NA, 6, NA, NA, NA, 1)
x <- data.frame(id, order, value)
Where ID represents each individual, order is the order of observations taken (1 is first, increasing by 1 with subsequent observations) and value is some value that needs backfilling (i.e. I need the NA's for each ID to be filled in a value if it is missing).
I am stuck on how to do this. I tried ordering the data first:
x <- x[order(x$id, -x$order, x$value),]
But I could not fathom how to get the code to work by selecting the previous observation that way (i.e. if id = previous id & value is missing, take previous version of value). The data is large (13m records) and there are many different numbers of order there (most have 1 observation, some may have up to 10). What would be the best way to do this?