R 用条件用前一行值填充 NA

R fill in NA with previous row value with condition

我需要用前一行值填充 NA 行,但仅在条件未更改之前。 作为星期几、膳食和价格的简单示例:

Day = c("Mon", "Tues", "Wed", "Thus", "Fri", "Sat","Sun","Mon", "Tues", 
       "Wed", "Thus", "Fri", "Sat","Sun") 
Meal = c("B","B","B","B","B","D","D","D","D","L","L", "L","L","L") 
Price = c(NA, 20, NA,NA,NA,NA,NA,15,NA,NA,10,10,NA,10) 
df = data.frame(Meal,Day ,Price )
df
   Meal  Day Price
1     B  Mon    NA
2     B Tues    20
3     B  Wed    NA
4     B Thus    NA
5     B  Fri    NA
6     D  Sat    NA
7     D  Sun    NA
8     D  Mon    15
9     D Tues    NA
10    L  Wed    NA
11    L Thus    10
12    L  Fri    10
13    L  Sat    NA
14    L  Sun    10

我需要用之前的 NA 填写 NA,但仅限于一周内相同的膳食类型。

我试过了

     na.locf(df, fromLast = TRUE)
   Meal  Day Price
1     B  Mon    20
2     B Tues    20
3     B  Wed    15
4     B Thus    15
5     B  Fri    15
6     D  Sat    15
7     D  Sun    15
8     D  Mon    15
9     D Tues    10
10    L  Wed    10
11    L Thus    10
12    L  Fri    10
13    L  Sat    10
14    L  Sun    10

这是错误的,因为与膳食类型重叠。 数据应如下所示:

  Meal  Day Price
1     B  Mon    20
2     B Tues    20
3     B  Wed    20
4     B Thus    20
5     B  Fri    20
6     D  Sat    15
7     D  Sun    15
8     D  Mon    15
9     D Tues    15
10    L  Wed    10
11    L Thus    10
12    L  Fri    10
13    L  Sat    10
14    L  Sun    10

非常感谢

你可以试试

library(zoo)
library(dplyr)
df %>% 
     group_by(Meal) %>% 
     mutate(Price= ifelse(any(!is.na(Price)), na.locf(na.locf(Price, 
                    fromLast=TRUE, na.rm=FALSE)), NA_real_))
#     Meal  Day Price
#1     B  Mon    20
#2     B Tues    20
#3     B  Wed    20
#4     B Thus    20
#5     B  Fri    20
#6     D  Sat    15
#7     D  Sun    15
#8     D  Mon    15
#9     D Tues    15
#10    L  Wed    10
#11    L Thus    10
#12    L  Fri    10
#13    L  Sat    10
#14    L  Sun    10

另一个选项使用 data.table

library(data.table)
library(xts)

dt <- data.table(df)

dt[, Price := na.locf(Price, fromLast = TRUE), by = Meal]

这个怎么样?

library(dplyr)

df %>% group_by(Meal) %>% fill( Price, .direction = 'updown' )