R fill in NA with previous row value with condition

前端 未结 2 1772
深忆病人
深忆病人 2021-02-10 05:49

I need to fill in NA rows with the previous row value, but only until a criteria is not changed. As a simple example for days of week, meals and prices:

Day = c(         


        
2条回答
  •  鱼传尺愫
    2021-02-10 06:09

    Another option using data.table

    library(data.table)
    library(xts)
    
    dt <- data.table(df)
    
    dt[, Price := na.locf(Price, fromLast = TRUE), by = Meal]
    

提交回复
热议问题