Zico Zico - 1 month ago 6
R Question

Population of last row value based on match condition in R

I have following input data.

Time Flag Spread
2016-06-01 09:30:01 new 0
2016-06-01 09:30:04 matched 0.04
2016-06-01 09:30:05 new 0
2016-06-01 09:30:06 new 0
2016-06-01 09:31:02 matched 0.05
2016-06-01 09:31:08 matched 0.04
2016-06-01 09:32:03 matched 0.05
2016-06-01 09:33:09 matched 0.01
2016-06-01 09:34:01 new 0
2016-06-01 09:35:03 matched 0.12
2016-06-01 09:35:04 new 0
2016-06-01 09:35:06 matched 0.08
2016-06-01 09:35:09 matched 0.05


I am looking for below output from above input.

Time Flag Spread
2016-06-01 09:30:01 new 0
2016-06-01 09:30:04 matched 0.04
2016-06-01 09:30:05 new 0.04
2016-06-01 09:30:06 new 0.04
2016-06-01 09:31:02 matched 0.05
2016-06-01 09:31:08 matched 0.04
2016-06-01 09:32:03 matched 0.05
2016-06-01 09:33:09 matched 0.01
2016-06-01 09:34:01 new 0.01
2016-06-01 09:35:03 matched 0.12
2016-06-01 09:35:04 new 0.12
2016-06-01 09:35:06 matched 0.08
2016-06-01 09:35:09 matched 0.05


Algo: Rows with
Flag == "new"
will be populating value of
Spread
column from last
Flag == "match"
row. Last
match
means it is from time-sorted list.

Algo Update: If first row/first few rows of data is Flag == "new",
then Spread of those rows will remain 0.


Can you please suggest simple
R
implementation for the same.

Update: Updated first row of input and output data as well as Algo,
to reflect updated query based on primary reply from @Gregor.

Answer
your_data$Spread = zoo::na.locf(ifelse(
    your_data$Flag == "new", NA, your_data$Spread
))

This is a straightforward application of "last observation carried forward". I assume your data is already sorted in the order you want.