R fill in NA with previous row value with condition

前端 未结 2 1061
孤独总比滥情好
孤独总比滥情好 2021-02-10 05:39

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:19

    Another option using data.table

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

提交回复
热议问题