错误位置必须介于0和n之间的函数中,按日期范围过滤和求和

时间:2017-03-06 01:20:27

标签: r dplyr

对于以下样本数据集:

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中对给定指标求和?

1 个答案:

答案 0 :(得分:0)

select(metric)更改为select_(metric),并在调用函数sumByDates(Sys.Date()-5, Sys.Date()-1, "Sessions")时将度量标准设为字符串。

根据&#34;非标准评估&#34;在DPLYR