【问题标题】:Week of year using bigquery / bigrquery and dbplyr? (equivalent of lubridate::week)一年中的一周使用 bigquery / bigrquery 和 dbplyr? (相当于 lubridate::week)
【发布时间】:2019-12-06 02:11:23
【问题描述】:

我正在尝试使用 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 获取周数的方法

【问题讨论】:

    标签: r google-bigquery tidyverse dbplyr bigrquery


    【解决方案1】:

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

    看起来您正在寻找以下 BigQuery 标准 SQL 函数

    EXTRACT(WEEK FROM date)    
    

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

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

    【讨论】:

    • dbplyr 从标准的 dplyr 命令转换为数据库语言(在您的情况下为 BigQuery)。 lubridate::week() 的翻译似乎不存在,因为它不是 dplyr 核心的一部分。请参阅 stackoverflow.com/questions/55348039/… 了解利用此功能的方法。
    • @Simon.S.A. - 我认为您的意思是对问题发表评论,而不是对我的回答 :o) 或者您可以发布您的答案
    猜你喜欢
    • 2020-03-06
    • 1970-01-01
    • 2023-03-13
    • 2013-11-15
    • 1970-01-01
    • 1970-01-01
    • 2012-10-31
    • 2023-03-23
    • 1970-01-01
    相关资源
    最近更新 更多