【发布时间】:2021-03-27 06:29:39
【问题描述】:
我的数据如下:
library(data.table)
DT <- structure(list(country = c("Germany", "Germany", "Germany", "Germany",
"Germany", "France", "France", "France", "France", "France",
"UK", "UK", "UK", "UK", "UK"), year = c(2000, 2001, 2002, 2003,
2004, 2000, 2001, 2002, 2003, 2004, 2000, 2001, 2002, 2003, 2004
), a = c(NA, NA, NA, NA, NA, NA, 1000, NA, 1600, NA, 1000, NA,
1000, NA, NA), b = c(NA, NA, NA, NA, NA, NA, 1000, NA, 2200,
NA, 1000, NA, 1000, NA, NA)), row.names = c(NA, -15L), class = c("data.table",
"data.frame"))
我正在尝试插入一些值。
我试图重现这个错误(这当然是一个非常合乎逻辑的错误,因为没有德国的数据):
Error: Problem with `mutate()` input `a`.
x need at least two non-NA values to interpolate
i Input `count` is `(structure(function (..., .x = ..1, .y = ..2, . = ..1) ...`.
i The error occurred in group 123: value = "group", country = "Arthur", State = "Nebraska".
Run `rlang::last_error()` to see where the error occurred.
使用以下代码:
library(tidyverse)
library(zoo)
st_example %>%
group_by(country) %>%
mutate_at(vars(a),~na.fill(.x,c(NA, "extend", NA))) %>%
filter(!is.na(a))
很遗憾,我没有收到预期的错误。
我的问题是当mutate_at 失败时,它不会继续下一组。我试图在mutate_at(vars(a:b),~na.fill(.x,c(NA, "extend", NA))) %>% 周围构建一个tryCatch,但没有成功。
如果遇到错误,我如何告诉mutate_at 继续?
【问题讨论】:
标签: r syntax interpolation na dplyr