【问题标题】:How group date as day in r如何在 r 中将日期分组为一天
【发布时间】:2021-02-16 04:08:50
【问题描述】:

我是。 尝试将每个日期下的交易分组为一天 例如,对于 2021 年 2 月 13 日以下的所有交易,应归为星期三

Recipient Network Provider Type FaceValue Date
24254432670 Amtel Gold Airtime 350 15/02/2021 23:59
24254432670 Amtel Gold Airtime 300 15/02/2021 23:56
24254432670 Amtel Gold Airtime 200 15/02/2021 23:54
24254432670 Amtel Gold Airtime 2200 14/02/2021 23:54
24254432670 Amtel Gold Airtime 500 14/02/2021 23:54
24254432670 Amtel Gold Airtime 100 14/02/2021 23:54
24254432670 Amtel Gold Airtime 200 14/02/2021 23:54
24254432670 Amtel Gold Airtime 1200 14/02/2021 23:54
24254432670 Amtel Gold Airtime 200 14/02/2021 23:54
24254432670 Amtel Gold Airtime 100 14/02/2021 23:54
24254432670 Amtel Gold Airtime 1500 13/02/2021 23:46
24254432670 Amtel Gold Airtime 200 13/02/2021 23:46
24254432670 Amtel Gold Airtime 2000 13/02/2021 23:46
24254432670 Amtel Gold Airtime 500 13/02/2021 23:46
24254432670 Amtel Gold Airtime 300 13/02/2021 23:46
24254432670 Amtel Gold Airtime 400 13/02/2021 23:46
24254432670 Amtel Gold Airtime 2000 13/02/2021 23:46
24254432670 Amtel Gold Airtime 100 13/02/2021 23:46
24254432670 Amtel Gold Airtime 1000 15/02/2021 23:40
8104573975 Amtel GOLAD Airtime 2000 15/02/2021 23:40

【问题讨论】:

  • '13/02/2021' 是星期六。你需要weekdays(as.Date('13/02/2021', '%d/%m/%Y')) 吗?
  • 我想要的是所有 13 号以下的交易都应该组合在一起并在星期六调用......谢谢
  • df$Weekday <- weekdays(as.Date(df$FaceValue '%d/%m/%Y')) ?你能显示预期的输出吗?
  • 好的,我就可以了..我会尽快提供反馈,谢谢
  • 仍然无法正常工作,请参阅下面的错误错误:意外符号在:“weekdays(as.Date(bindingall$Date, '%d/%m/%Y') weekdays"

标签: r tidyverse lubridate


【解决方案1】:
# your data
df <- tribble(
  ~Recipient, ~Network, ~Provider, ~Type, ~FaceValue, ~Date,
"24254432670", "Amtel", "Gold", "Airtime", "350", "15/02/2021 23:59", 
  "24254432670", "Amtel", "Gold", "Airtime", "300", "15/02/2021 23:56", 
  "24254432670", "Amtel", "Gold", "Airtime", "200", "15/02/2021 23:54", 
  "24254432670", "Amtel", "Gold", "Airtime", "2200", "14/02/2021 23:54", 
  "24254432670", "Amtel", "Gold", "Airtime", "500", "14/02/2021 23:54", 
  "24254432670", "Amtel", "Gold", "Airtime", "100", "14/02/2021 23:54", 
  "24254432670", "Amtel", "Gold", "Airtime", "200", "14/02/2021 23:54", 
  "24254432670", "Amtel", "Gold", "Airtime", "1200", "14/02/2021 23:54", 
  "24254432670", "Amtel", "Gold", "Airtime", "200", "14/02/2021 23:54", 
  "24254432670", "Amtel", "Gold", "Airtime", "100", "14/02/2021 23:54", 
  "24254432670", "Amtel", "Gold", "Airtime", "1500", "13/02/2021 23:46", 
  "24254432670", "Amtel", "Gold", "Airtime", "200", "13/02/2021 23:46", 
  "24254432670", "Amtel", "Gold", "Airtime", "2000", "13/02/2021 23:46", 
  "24254432670", "Amtel", "Gold", "Airtime", "500", "13/02/2021 23:46", 
  "24254432670", "Amtel", "Gold", "Airtime", "300", "13/02/2021 23:46", 
  "24254432670", "Amtel", "Gold", "Airtime", "400", "13/02/2021 23:46", 
  "24254432670", "Amtel", "Gold", "Airtime", "2000", "13/02/2021 23:46", 
  "24254432670", "Amtel", "Gold", "Airtime", "100", "13/02/2021 23:46", 
  "24254432670", "Amtel", "Gold", "Airtime", "1000", "15/02/2021 23:40", 
  "8104573975", "Amtel", "GOLAD", "Airtime", "2000", "15/02/2021 23:40")

# manipulate and create new column where 13/02/2021 is Saturday
df <- df %>% 
  mutate(Recipient=as.numeric(Recipient),
         FaceValue = as.numeric(FaceValue),
         Date = format(as.POSIXct(strptime(Date,"%d/%m/%Y %H:%M",tz="")) ,format = "%d/%m/%Y"),
         NewCol = case_when(Date == "13/02/2021" ~ "Saturday")
         )

【讨论】:

    【解决方案2】:

    编辑鉴于another question

    library(dplyr)
    
    df %>% mutate(DAY = as.Date(Date, "%d/%m/%Y"),
                  DAY = paste0('Day', dense_rank(DAY)))
    
    # A tibble: 20 x 7
       Recipient   Network Provider Type    FaceValue Date             DAY  
       <chr>       <chr>   <chr>    <chr>   <chr>     <chr>            <chr>
     1 24254432670 Amtel   Gold     Airtime 350       15/02/2021 23:59 Day3 
     2 24254432670 Amtel   Gold     Airtime 300       15/02/2021 23:56 Day3 
     3 24254432670 Amtel   Gold     Airtime 200       15/02/2021 23:54 Day3 
     4 24254432670 Amtel   Gold     Airtime 2200      14/02/2021 23:54 Day2 
     5 24254432670 Amtel   Gold     Airtime 500       14/02/2021 23:54 Day2 
     6 24254432670 Amtel   Gold     Airtime 100       14/02/2021 23:54 Day2 
     7 24254432670 Amtel   Gold     Airtime 200       14/02/2021 23:54 Day2 
     8 24254432670 Amtel   Gold     Airtime 1200      14/02/2021 23:54 Day2 
     9 24254432670 Amtel   Gold     Airtime 200       14/02/2021 23:54 Day2 
    10 24254432670 Amtel   Gold     Airtime 100       14/02/2021 23:54 Day2 
    11 24254432670 Amtel   Gold     Airtime 1500      13/02/2021 23:46 Day1 
    12 24254432670 Amtel   Gold     Airtime 200       13/02/2021 23:46 Day1 
    13 24254432670 Amtel   Gold     Airtime 2000      13/02/2021 23:46 Day1 
    14 24254432670 Amtel   Gold     Airtime 500       13/02/2021 23:46 Day1 
    15 24254432670 Amtel   Gold     Airtime 300       13/02/2021 23:46 Day1 
    16 24254432670 Amtel   Gold     Airtime 400       13/02/2021 23:46 Day1 
    17 24254432670 Amtel   Gold     Airtime 2000      13/02/2021 23:46 Day1 
    18 24254432670 Amtel   Gold     Airtime 100       13/02/2021 23:46 Day1 
    19 24254432670 Amtel   Gold     Airtime 1000      15/02/2021 23:40 Day3 
    20 8104573975  Amtel   GOLAD    Airtime 2000      15/02/2021 23:40 Day3
    

    较早的答案

    为什么不像 Ronak 在他的评论中所建议的那样简单地这样做呢? (假设df你的数据)

    #Either
    df$DAY <- weekdays(as.Date(df$Date, "%d/%m/%Y"))
    
    #OR
    dplyr::mutate(df, DAY = weekdays(as.Date(Date, "%d/%m/%Y")))
    
    # A tibble: 20 x 7
       Recipient   Network Provider Type    FaceValue Date             DAY     
       <chr>       <chr>   <chr>    <chr>   <chr>     <chr>            <chr>   
     1 24254432670 Amtel   Gold     Airtime 350       15/02/2021 23:59 Monday  
     2 24254432670 Amtel   Gold     Airtime 300       15/02/2021 23:56 Monday  
     3 24254432670 Amtel   Gold     Airtime 200       15/02/2021 23:54 Monday  
     4 24254432670 Amtel   Gold     Airtime 2200      14/02/2021 23:54 Sunday  
     5 24254432670 Amtel   Gold     Airtime 500       14/02/2021 23:54 Sunday  
     6 24254432670 Amtel   Gold     Airtime 100       14/02/2021 23:54 Sunday  
     7 24254432670 Amtel   Gold     Airtime 200       14/02/2021 23:54 Sunday  
     8 24254432670 Amtel   Gold     Airtime 1200      14/02/2021 23:54 Sunday  
     9 24254432670 Amtel   Gold     Airtime 200       14/02/2021 23:54 Sunday  
    10 24254432670 Amtel   Gold     Airtime 100       14/02/2021 23:54 Sunday  
    11 24254432670 Amtel   Gold     Airtime 1500      13/02/2021 23:46 Saturday
    12 24254432670 Amtel   Gold     Airtime 200       13/02/2021 23:46 Saturday
    13 24254432670 Amtel   Gold     Airtime 2000      13/02/2021 23:46 Saturday
    14 24254432670 Amtel   Gold     Airtime 500       13/02/2021 23:46 Saturday
    15 24254432670 Amtel   Gold     Airtime 300       13/02/2021 23:46 Saturday
    16 24254432670 Amtel   Gold     Airtime 400       13/02/2021 23:46 Saturday
    17 24254432670 Amtel   Gold     Airtime 2000      13/02/2021 23:46 Saturday
    18 24254432670 Amtel   Gold     Airtime 100       13/02/2021 23:46 Saturday
    19 24254432670 Amtel   Gold     Airtime 1000      15/02/2021 23:40 Monday  
    20 8104573975  Amtel   GOLAD    Airtime 2000      15/02/2021 23:40 Monday
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多