一年中的一周使用bigquery / bigrquery和dbplyr? (相当于lubridate :: week)

时间:2019-12-06 02:11:23

标签: r google-bigquery tidyverse dbplyr bigrquery

我正在尝试使用bigrquery和dbplyr获取week of the year that a date corresponds to(即与lubridate::week()相同,即

library(lubridate)
library(dbplyr)
library(bigrquery)

week("2015-08-11")   
# [1] 32

但是我正在使用bigrquerydbplyr

到目前为止我已经尝试过的

使用lubridate::week(),我知道

transactions %>% 
  select(item, date) %>% 
  mutate(week = week(date)) %>%
  collect()
Error: Function not found: week at [1:30] [invalidQuery]

所以我尝试了这种自制解决方案

transactions %>% 
  select(item, date) %>%
  mutate(week = strftime(date, format = "%V")) %>% 
  collect()

Error: Syntax error: Expected ")" but got keyword AS at [1:54] [invalidQuery]
In addition: Warning message:
  Named arguments ignored for SQL strftime 

以及另一个(非常丑陋的)自制解决方案

transactions %>% 
  select(item, date) %>% 
  mutate(week = as.numeric((as.Date(date) - as.Date(paste0(substr(date, 1, 4), "-01-01"))), units="days") %/% 7) %>% 
  collect()

Error in as.numeric((as.Date(date) - as.Date(paste0(substr(date, 1,  : 
unused argument (units = "days")

但是我似乎找不到使用bigquery和dbplyr获取星期数的方法

1 个答案:

答案 0 :(得分:2)

  

我似乎找不到使用bigquery获取星期数的方法

看起来像您在下面的BigQuery Standard SQL函数

EXTRACT(WEEK FROM date)    

您可以使用WEEK或WEEK()或ISOWEEK

https://cloud.google.com/bigquery/docs/reference/standard-sql/date_functions#extract上查看有关日期部分的更多信息