对于以下样本数据集:
set.seed(123)
library(dplyr)
Sessions <- ceiling(rnorm(50, mean = 3000, sd = 300))
Registrations <- ceiling(rnorm(50, mean = 800, sd = 100))
Conversions <- ceiling(rnorm(50, mean = 400, sd = 50))
Date_Range = seq(Sys.Date() - 50, Sys.Date() - 1, by = 1)
sample_data <- data.frame(Date_Range,Sessions, Registrations, Conversions)
sample_data <- tbl_df(sample_data)
我有以下功能,旨在提供日期范围的给定指标的总和:
sumByDates <- function(startDate, endDate, metric) {
sum(filter(sample_data, Date_Range >= startDate & Date_Range <= endDate) %>% select(metric))
}
现在,如果我这样做:
sumByDates(Sys.Date()-5, Sys.Date()-1, Sessions)
我会得到:
Error: Position must be between 0 and n
Called from: combine_vars(vars, ind_list)
Browse[1]> Q
对于给定的sample_data
,如何在Date_Range
中对给定指标求和?
答案 0 :(得分:0)
将select(metric)
更改为select_(metric)
,并在调用函数sumByDates(Sys.Date()-5, Sys.Date()-1, "Sessions")
时将度量标准设为字符串。
根据&#34;非标准评估&#34;在DPLYR