使用 na.spline() 和 case_when() 按组进行插值和外推

Interpolate and extrapolated by group using na.spline() and case_when()

我缺少一些日期,我想在每一组内插值和外推值,即使我只有一个值可用。

#create an example
library(zoo)
library(tidyverse)

df <- data.frame(a = c("group1","group1","group1","group1","group2","group2","group2","group2","group3","group3"), b = c(1,2,NA,4,1,NA,NA,NA,NA,NA))
        a  b
1  group1  1
2  group1  2
3  group1 NA
4  group1  4
5  group2  1
6  group2 NA
7  group2 NA
8  group2 NA
9  group3 NA
10 group3 NA

我想获得这个:

        a  b b_interpolated
1  group1  1              1
2  group1  2              2
3  group1 NA              3
4  group1  4              4
5  group2  1              1
6  group2 NA              1
7  group2 NA              1
8  group2 NA              1
9  group3 NA             NA
10 group3 NA             NA

首先,我尝试按组na.spline使用

df %>% group_by(a) %>% mutate(b_interpolated = na.spline(b, na.rm = FALSE))

并给我这个错误:

Error: Problem with `mutate()` input `test`.
x zero non-NA points
ℹ Input `test` is `na.spline(b, na.rm = FALSE)`.
ℹ The error occurred in group 3: a = "group3".

所以我尝试在任何值可用时使用 na.spline

#Interpolate and extrapolate test
test <- df %>% group_by(a) %>% mutate(test = case_when(all(is.na(b)) == TRUE ~ "empty",
                                                       all(is.na(b)) == FALSE ~ "ok"))

这似乎可行,但如果我尝试使用 na.spline:

test2 <- df %>% group_by(a) %>% mutate(b_interpolated = case_when(all(is.na(b)) == TRUE ~ b,
                                                       all(is.na(b)) == FALSE ~ na.spline(b, na.rm = FALSE)))

然后,又报错了:

Error: Problem with `mutate()` input `b_interpolated`.
x zero non-NA points
ℹ Input `b_interpolated` is `case_when(...)`.
ℹ The error occurred in group 3: a = "group3"

如果我使用 na.approx,group2 无法外推,因为只有一个值

df %>% group_by(a) %>% mutate(b_interpolated = na.approx(b, na.rm = FALSE, rule = 2))
   a          b b_interpolated
   <chr>  <dbl>          <dbl>
 1 group1     1              1
 2 group1     2              2
 3 group1    NA              3
 4 group1     4              4
 5 group2     1              1
 6 group2    NA             NA
 7 group2    NA             NA
 8 group2    NA             NA
 9 group3    NA             NA
10 group3    NA             NA

我不明白为什么使用 case_when 会报错,我确定我遗漏了什么...

这看起来像是 na.spline 中的错误。使用它来解决它。

在 na.approx 的情况下,我们使用 na.fill 将数据扩展到开头和结尾的 NA。 na.fill 的第二个参数是一个 3 向量,它给出了左端、内部 NA 和右端的替换规则。它循环利用,所以我们可以省略右端。

na_spline <- function(x) if (all(is.na(x))) NA else na.spline(x, na.rm = FALSE)
na_approx <- function(x) na.fill(na.approx(x, na.rm = FALSE), c("extend", NA))

df %>%
  group_by(a) %>%
  mutate(spline = na_spline(b), approx = na_approx(b)) %>%
  ungroup

给予:

# A tibble: 10 x 4
   a          b spline approx
   <chr>  <dbl>  <dbl>  <dbl>
 1 group1     1      1      1
 2 group1     2      2      2
 3 group1    NA      3      3
 4 group1     4      4      4
 5 group2     1      1      1
 6 group2    NA      1      1
 7 group2    NA      1      1
 8 group2    NA      1      1
 9 group3    NA     NA     NA
10 group3    NA     NA     NA