r - Dplyr 中的标准和非标准评估
问题描述
亲爱的同事,我正在尝试构建一个在数据框中线性插值数据的函数:
代码如下所示:
Linear_Interpolation <- function(df, min_ts, max_ts, target_column, signal_key) {
if (exists(deparse(substitute(df))) == TRUE) {
if (nrow(df) != 0) {
vector.sequences <- seq(from = min_ts,
to = max_ts,
by = "hour")
df.interpolation.aux <- data.table(snsr_ts = vector.sequences)
df.interpolated <- bind_rows(df, df.interpolation.aux) %>%
arrange(., snsr_ts)
df.duplicates <- which(duplicated((df.interpolated$snsr_ts)))
df.interpolated <- df.interpolated[-df.duplicates,] %>%
mutate_(., column = na.approx(column)) %>%
mutate(., snsr_dt = as.Date(snsr_ts)) %>%
mutate(., package = aux1$package) %>%
rename_at(snsr_val = column) %>%
mutate(snsr_key = signal_key) %>%
mutate(locf_tag='N') %>%
mutate(qlty_good_ind=ifelse(is.na(qlty_good_ind)==TRUE, 'Y', qlty_good_ind)) %>%
mutate(qlty_interp=ifelse(is.na(qlty_interp)==TRUE, -3, qlty_interp))
}
} else {
df.interpolated <- NULL
}
return(df.interpolated)
}
当我使用 dplyr 时,我知道我不能使用标准评估。但是,当我尝试使用 mutate_ 时,我收到消息,现在此功能已被弃用。因此,我尝试遵循https://dplyr.tidyverse.org/articles/programming.html指南并使用以下版本:
Linear_Interpolation <- function(df, min_ts, max_ts, target, signal_key) {
if (exists(deparse(substitute(df))) == TRUE) {
if (nrow(df) != 0) {
target <- enquo(target)
signal_key <- enquo(signal_key)
vector.sequences <- seq(from = min_ts,
to = max_ts,
by = "hour")
df.interpolation.aux <- data.table(snsr_ts = vector.sequences)
df.interpolated <- bind_rows(df, df.interpolation.aux) %>%
arrange(., snsr_ts)
df.duplicates <- which(duplicated((df.interpolated$snsr_ts)))
df.interpolated <- df.interpolated[-df.duplicates,] %>%
mutate(snsr_val = na.approx(!!target)) %>%
mutate(snsr_dt = as.Date(snsr_ts)) %>%
mutate(., package = aux1$package) %>
mutate(snsr_key = !!signal_key) %>%
mutate(locf_tag='N') %>%
mutate(qlty_good_ind=ifelse(is.na(qlty_good_ind)==TRUE, 'Y', qlty_good_ind)) %>%
mutate(qlty_interp=ifelse(is.na(qlty_interp)==TRUE, -3, qlty_interp))
}
} else {
df.interpolated <- NULL
}
return(df.interpolated)
}
但是我得到以下结果:
df.interpolated.final <- Linear_Interpolation(df, min(df$snsr_ts), max(df$snsr_ts), "column_name", "71")
Error in xy.coords(x, y, setLab = FALSE) :
'pairlist' object cannot be coerced to type 'double'
In addition: Warning message:
In is.na(y) :
Error in xy.coords(x, y, setLab = FALSE) :
'pairlist' object cannot be coerced to type 'double'
>
我感觉目标正在读取为 na_approx 函数中的文本,即使我无法完全调试它。输入数据帧如下:
snsr_dt package value snsr_ts locf_tag db_src qlty_interp qlty_good_ind
8/26/2011 589 0 8/26/11 12:00 N 2 1 Y
10/4/2013 589 147 10/4/13 0:00 N 2 1 Y
10/17/2014 589 160 10/17/14 0:00 N 2 1 Y
11/14/2015 589 168 11/14/15 0:00 N 2 1 Y
12/28/2016 589 198 12/28/16 0:00 N 2 1 Y
1/10/2018 589 215 1/10/18 0:00 N 2 1 Y
1/4/2019 589 238 1/4/19 0:00 N 2 1 Y
有人知道发生了什么吗?
解决方案
因为您想要接受字符串和符号,所以正确的动词是ensym()
,而不是enquo()
。这是一个最小的可重现示例,您可以适应更大的应用程序:
library( tidyverse )
library( lubridate )
Linear_Interpolation <- function( df, target ) {
vseq <- seq( from=min(df$date), to=max(df$date), by="day")
tibble(date = vseq) %>%
left_join(df, by="date") %>%
mutate( snsr_val = zoo::na.approx(!!ensym(target)) ) # <--- ensym
}
## Data
X <- tibble( date = mdy("10/4/2013", "10/7/2013", "10/12/2013"),
value = c(0, 147, 160) )
Linear_Interpolation( X, "value" ) # Works on strings
Linear_Interpolation( X, value ) # ...and symbols
# date value snsr_val
# <date> <dbl> <dbl>
# 1 2013-10-04 0 0
# 2 2013-10-05 NA 49
# 3 2013-10-06 NA 98
# 4 2013-10-07 147 147
# 5 2013-10-08 NA 150.
# 6 2013-10-09 NA 152.
# 7 2013-10-10 NA 155.
# 8 2013-10-11 NA 157.
# 9 2013-10-12 160 160
推荐阅读
- python - python 使用 selenium,错误:chrome 意外退出。状态码是:0
- mysql - 透视 MySQL 表设置布尔值
- ffmpeg - 如何加快声音和视频并修改音高?
- java - 如何在水平滚动视图中添加许多列表视图?
- c# - 为什么在 Akka.net 中以类的形式发送消息是一种好习惯?
- python - Y-ticks 没有出现在 matplotlib 中
- swagger - 如何使用响应式路由在 Quarkus 中启用 swagger iu?
- reactjs - Javascript React 可以将 HttpOnly cookie 发送到服务器吗?
- ignite - Apache Ignite 授权
- python - 我的 Python 应用程序在 eureka 服务器成功注册后无法打开