将SQL脚本转换为tidyverse格式

时间:2019-01-18 15:57:52

标签: r dplyr tidyverse sqldf

我正在尝试将前同事的脚本从sqldf重写为tidyverse。我不能遵循这一块。两个名为“ data”和“ Stt_nm”的数据帧被合并,但是我不明白为什么他使用了两个选择。也许我在这里错过了一些东西。

output = sqldf(
  paste0(
    "select a.date,b.name,sum(a.weigh) as rch
     from (select distinct date,ID,IndID,Station_id,weigh from data) as a
          left join Stt_nm as b on a.Station_id=b.Station
     where a.date between ",
     startdate,
     " and ",
     enddate,
     "
     group by a.date,b.name"
  )
)

1 个答案:

答案 0 :(得分:1)

如果没有可复制的示例,我无法确定,但是我认为应该这样做:

library(tidyverse)
data %>% 
  select(date, ID, IndID, Station_id, weigh) %>% 
  left_join(Stt_nm, by="Station") %>%
  filter(date <= startdate & date >= enddate) %>%
  group_by(date, name) %>%
  summarize(rch = sum(weigh))