【问题标题】:Identifying weeks from complex date format [duplicate]从复杂的日期格式中识别星期[重复]
【发布时间】:2020-10-25 14:10:35
【问题描述】:

我对数据框中的日期有一种非常烦人的格式。这是一个示例:

"Jan 1, 2020, 8:36:55 PM"  "Jan 7, 2020, 12:00:00 PM" "Jan 9, 2020, 8:24:55 PM" 

我要做的第一件事就是按年份过滤它。我最终只使用了grep(),因为没有出现 2020 的其他上下文,但这不是一个优雅的解决方案。我希望我当前问题的答案也能对此有所帮助。

无论如何,我现在想确定周数。我想按周计算不同列的每个单元格的总和。但是,我什至不知道如何将该字符串转换为某种日期...

只是给你一个我的数据样本,应该是这样的(已经过滤到 2020 年):

               Activity.Date Moving.Time
1    Jan 1, 2020, 8:36:55 PM        3581
2   Jan 7, 2020, 12:00:00 PM        1200
3    Jan 9, 2020, 8:24:55 PM         970
4   Jan 12, 2020, 7:51:30 PM        5564
5    Feb 4, 2020, 9:20:21 AM        1350
6    Feb 5, 2020, 9:20:00 AM        2400
7    Feb 6, 2020, 9:15:00 AM        2415
8  Feb 16, 2020, 11:55:51 AM        1836
9   Feb 17, 2020, 8:36:47 PM         511
10  Feb 25, 2020, 7:30:00 PM         928
11   Mar 4, 2020, 7:41:02 PM         558
12   Mar 6, 2020, 8:25:27 PM        2637
13   Mar 9, 2020, 8:37:11 PM         577
14  Mar 11, 2020, 7:46:10 PM         523
15 Mar 11, 2020, 10:00:25 PM        1278
16 Mar 12, 2020, 12:34:41 AM         442
17  Mar 13, 2020, 8:26:55 PM        2410
18  Mar 16, 2020, 8:25:22 PM         609
19  Sep 12, 2020, 7:27:26 PM        1884
20  Sep 15, 2020, 7:46:27 PM        1783
21  Sep 17, 2020, 8:41:19 PM        1838
22 Sep 19, 2020, 12:08:56 PM        1995
23  Sep 22, 2020, 7:29:01 PM        1776
24  Sep 24, 2020, 7:08:35 PM        1972
25  Sep 26, 2020, 7:24:52 PM        4032
26   Oct 3, 2020, 7:27:22 PM        4172
27   Oct 7, 2020, 8:00:41 PM        2987
28   Oct 8, 2020, 6:57:21 PM        2319
29  Oct 10, 2020, 7:23:39 PM        2509
30  Oct 12, 2020, 6:54:36 PM        5711
31  Oct 13, 2020, 7:56:59 PM        1764
32  Oct 14, 2020, 7:18:06 PM        4822
33  Oct 15, 2020, 8:09:31 PM        1863
34  Oct 17, 2020, 7:50:45 PM        5086
35  Oct 20, 2020, 7:58:39 PM        1583
36  Oct 21, 2020, 8:16:10 PM        4978
37  Oct 22, 2020, 7:23:26 PM        1940
38  Oct 22, 2020, 8:18:24 PM        1857

编辑:如果可能的话,我还需要在第三列中求和的行数......

【问题讨论】:

    标签: r datetime format


    【解决方案1】:

    您可以使用as.POSIXct。

    x <- c("Jan 1, 2020, 8:36:55 PM",  "Jan 7, 2020, 12:00:00 PM", "Jan 9, 2020, 8:24:55 PM")
    as.POSIXct(x, format = '%b %d, %Y, %I:%M:%S %p', tz = 'UTC')
    #[1] "2020-01-01 20:36:55 UTC" "2020-01-07 12:00:00 UTC" "2020-01-09 20:24:55 UTC"
    

    ?strptime中提到了格式。

    如果这很难记住,您可以使用lubridate 中的mdy_hms。

    lubridate::mdy_hms(x)
    

    完成此操作后,您可以提取每周信息和sum Moving.Time。

    library(dplyr)
    library(lubridate)
    
    df %>%
      mutate(Activity.Date = mdy_hms(Activity.Date)) %>%
      group_by(Week = week(Activity.Date )) %>%
      summarise(Moving.Time =  sum(Moving.Time))
    

    【讨论】:

    • 好的,谢谢。但是有没有办法让我也添加一个总计有多少行的列?之前我没有意识到我需要它……
    • 您可以使用n() 计算每周的行数。将最后一行更改为summarise(Moving.Time = sum(Moving.Time), count = n())。
    【解决方案2】:

    使用以下方法将您的 activity.date 列转换为日期/时间对象:

    activitydate <-as.POSIXct("Jan 1, 2020, 8:36:55 PM", format="%b %d, %Y, %r")
    

    然后确定周数:

    format(activitydate, "%V") #or %U
    

    有关更多信息,请参阅strptime 的帮助。

    更新
    要回答关于提供行数的第二个问题,可以使用 dplyr 库轻松完成。

    df$Activity.Date <- as.POSIXct(df$Activity.Date, format="%b %d, %Y, %r")
    df$week <- format(df$Activity.Date, "%V")
    
    library(dplyr)
    df %>% group_by(week) %>% summarize(count=n(), sum=sum(Moving.Time))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-31
      相关资源
      最近更新 更多