【问题标题】:Is there a way to use sqldf Group By to return results by rolling periods in R?有没有办法使用 sqldf Group By 在 R 中按滚动周期返回结果?
【发布时间】:2020-09-30 03:45:03
【问题描述】:

我有一组数据,其中受访者每个月可以回答多次。

structure(list(Month = c("Jan 2016", "Jan 2016", "Feb 2016", 
"Feb 2016", "Mar 2016", "Apr 2016", "May 2016", "Jun 2016", "Jun 2016", 
"Jul 2016", "Aug 2016", "Aug 2016", "Sep 2016", "Sep 2016", "Oct 2016", 
"Nov 2016", "Dec 2016", "Dec 2016", "Jan 2016", "Feb 2016", "Feb 2016", 
"Feb 2016", "Mar 2016", "Mar 2016", "Apr 2016", "May 2016", "May 2016", 
"Jun 2016", "Jun 2016", "Jul 2016", "Aug 2016", "Aug 2016", "Oct 2016", 
"Oct 2016", "Dec 2016", "Mar 2016", "Mar 2016", "Apr 2016", "Apr 2016", 
"May 2016", "Jun 2016", "Aug 2016", "Sep 2016", "Jan 2016", "Jan 2016", 
"Feb 2016", "Feb 2016", "Feb 2016", "Feb 2016", "Feb 2016"), 
    PhysicianID = c(4263, 4263, 4263, 4263, 4263, 4263, 4263, 
    4263, 4263, 4263, 4263, 4263, 4263, 4263, 4263, 4263, 4263, 
    4263, 4278, 4278, 4278, 4278, 4278, 4278, 4278, 4278, 4278, 
    4278, 4278, 4278, 4278, 4278, 4278, 4278, 4278, 4282, 4282, 
    4282, 4282, 4282, 4282, 4282, 4282, 4309, 4309, 4309, 4309, 
    4309, 4309, 4309)), row.names = c(NA, -50L), class = c("tbl_df", 
"tbl", "data.frame"))

并且我需要知道滚动 3 个月期间的唯一受访者数量。每个月得到结果都不是问题:

sqldf("SELECT Month,COUNT(distinct(PhysicianID)) FROM Data_for_R GROUP BY Month")
      Month COUNT(distinct(PhysicianID))
1  Apr 2016                            3
2  Aug 2016                            3
3  Dec 2016                            2
4  Feb 2016                            3
5  Jan 2016                            3
6  Jul 2016                            2
7  Jun 2016                            3
8  Mar 2016                            3
9  May 2016                            3
10 Nov 2016                            1
11 Oct 2016                            2
12 Sep 2016                            2

我需要的是一种返回看起来更像的结果的方法

1 Jan 2016 to March 2016              xxx
2 Feb 2016 to April 2016              xxx
3 March 2016 to May 2016              xxx
etc...

【问题讨论】:

  • 问题已更新 - 道歉!
  • 抱歉,第一次发帖!

标签: r group-by sqldf


【解决方案1】:

将年/月转换为 yearmon 类(当发送到 SQL 时,1 月将显示为年 + 0,2 月显示为 1/12 等),然后将月份与匹配的 3 个尾随月份的数据相匹配浮点近似考虑。在当前月份分组并执行计数。只保留三个月的行——你可能想要也可能不想要。 name__class 方法将指示的类分配给具有 __ 后缀和类名的变量名。

library(sqldf)
library(zoo)

DFR <- transform(Data_for_R, Month = as.yearmon(Month, "%b %Y"))
Mos <- data.frame(Month = seq(min(DFR$Month), max(DFR$Month), 1/12))

sqldf("select 
    min(b.Month) From__yearmon, 
    max(b.Month) To__yearmon, 
    count(distinct b.PhysicianID) Num
  from 
    Mos a 
    left join DFR b 
      on b.Month between a.Month - 2./12.- 0.001 and a.Month + 0.001
  group by a.Month
  having count(distinct b.Month) = 3
", method = "name__class")

给予

       From       To Num
1  Jan 2016 Mar 2016   4
2  Feb 2016 Apr 2016   4
3  Mar 2016 May 2016   3
4  Apr 2016 Jun 2016   3
5  May 2016 Jul 2016   3
6  Jun 2016 Aug 2016   3
7  Jul 2016 Sep 2016   3
8  Aug 2016 Oct 2016   3
9  Sep 2016 Nov 2016   3
10 Oct 2016 Dec 2016   2

更新

已更新以处理缺少月份的情况。在问题中的样本数据上,它给出了与以前相同的答案,因为没有缺失月份。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多