且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用dplyr进行线性插值,但跳过所有缺失值的组

更新时间:2023-01-27 23:01:25

我们可以通过以下方法解决此问题:添加具有所需数据点数量的 filter 步骤:

We can fix this by adding a filter step with the required number of data points:

library(dplyr)
dataIpol <- data %>%
  group_by(id) %>% 
  arrange(id, year) %>%
  filter(sum(!is.na(value))>=2) %>% #filter!
  mutate(valueIpol = approx(year, value, year, 
                            method = "linear", rule = 1, f = 0, ties = mean)$y)

在这里,我们将value列中的非NA项目的数量相加,并删除所有不具有&gt ; = 2

Here we sum the number of non-NA items in the value column, and remove any groups that do not have >=2.