【问题标题】:How to select the members who has transacted on the last date of every month from a large data如何从大数据中选择每月最后一天交易的会员
【发布时间】:2020-02-18 08:51:39
【问题描述】:

我有每天交易的会员数据,我需要全年每个月最后一天交易的所有会员的列表。

我的输出需要包含在 2019 年 1 月 31 日、2019 年 2 月 28 日等到 2019 年 12 月 31 日进行过交易的成员列表(包含所有列)。

【问题讨论】:

标签: r dplyr lubridate


【解决方案1】:
month.ends <- as.Date(paste(unique(year(df$date)), unique(month(df$date)), "01", sep = "-"))-1

df %>% filter(date %in% month.ends)

如果您只想要其他一些列的唯一成员名称,您可以使用 distinct 函数

【讨论】:

    【解决方案2】:

    好的,这是一个效率很低的代码(对于 R 来说相对较新),但我认为如果你只想在 2019 年这样做,它就可以工作。

    #create a manual dataframe with the last days of the months in 2019
    LastDays <- structure(list(Date = structure(c(7L, 2L, 10L, 4L, 11L, 5L, 
    12L, 13L, 6L, 8L, 3L, 1L, 9L), .Label = c("10-12-2019", "28-2-2019", 
    "30-11-2019", "30-4-2019", "30-6-2019", "30-9-2019", "31-1-2019", 
    "31-10-2019", "31-12-2019", "31-3-2019", "31-5-2019", "31-7-2019", 
    "31-8-2019"), class = "factor")), class = "data.frame", row.names = c(NA, 
    -13L))
    
    #remove transactions on other dates in a new dataframe
    df_subset <- df[which(df$Date %in% LastDays$Date),]
    
    #find Members which did transactions on all the last days of the month
    Members <- df_subset %>% group_by(Member, Date) %>% summarise_all(funs(mean)) %>% select(Member, Date) %>% filter(n() >11)
    Members <- unique(Members$Member)
    
    #The information of all the members which transacted on all last dates of the year
    df[which(df$Member %in% Members),]
    

    【讨论】:

      猜你喜欢
      • 2019-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-03
      • 2018-09-21
      • 1970-01-01
      • 2020-07-18
      相关资源
      最近更新 更多