Say I have a data frame like this:
ID, ID_2, FIRST, VALUE
-----------------------
'a', 'aa', TRUE, 2
'a', 'ab', FALSE, NA
'a', 'ac', FALSE, NA
'b', 'aa', TRUE, 5
'b', 'ab', FALSE, NA
So VALUE is only set for FIRST = TRUE once per ID. ID_2 may be duplicate between IDs, but doesn’t have to.
How do I put the numbers from the first rows of each ID into all rows of that ID, such that the VALUE column becomes 2, 2, 2, 5, 5?
I know I could simply loop over all IDs with a for loop, but I am looking for a more efficient way.
If you need only to carry forward the values from the VALUE column, then I think you can use
na.lofc()function from zoo package. Here is an example: