【问题标题】:Fetching month and year from timestamps in R in a specific format and ordering them从 R 中的时间戳中以特定格式获取月份和年份并对其进行排序
【发布时间】:2023-03-31 16:47:01
【问题描述】:

该脚本将患者3个数据(以患者名称附加在包“bupaR”中)按患者列排列并计算时间列中对应时间戳的差异,然后以秒、分钟和小时为单位显示差异数据的最后三列。

我的要求是在数据集的倒数第三个位置创建一个列,从“May-2015”格式的时间列中获取月份和年份。例如。对于时间戳“2017-01-02 12:40:20”,我需要相应的新列值为“2017 年 1 月”,其他类似。此外,如果数据则可以按从“一月-YYYY”到“十二月-YYYY”格式的升序排列。

library(bupaR)
library(dplyr)
#Declare and assign the variables
patients1 <- arrange(patients, patient)
patients2 <- patients1 %>% arrange(patient, time)
patients3 <- patients2 %>%
group_by(patient) %>%
mutate(diff_in_sec = as.POSIXct(time, format = "%m/%d/%Y %H:%M") - 
lag(as.POSIXct(time, format = "%m/%d/%Y %H:%M"), 
default=first(as.POSIXct(time, format = "%m/%d/%Y %H:%M"))))%>%
mutate(diff_in_hours = as.numeric(diff_in_sec/3600)) %>% mutate(diff_in_days 
= as.numeric(diff_in_hours/24))

【问题讨论】:

    标签: r dplyr timestamp plyr posixct


    【解决方案1】:

    OP 可以附加dplyr 链来获得所需的输出。方法是:

    1.使用formatmonthyear 列添加为"%B-%Y"(即2017 年5 月)

    2.使用%m%Y (052017) 格式添加列monthyearNum。使用此列进行排序。然后最后将其从select中排除

    mutate(monthyear = format(time, format = "%B-%Y")) %%>
    mutate(monthyearNum = as.numeric(time, format = "%m%Y"))) %>%
    arrange(monthyearNum) %>%
    select(-monthyearNum)
    

    【讨论】:

      【解决方案2】:

      这有帮助吗?

      library(bupaR)
      library(dplyr)
      
      patients %>%
        data.frame() %>%
        arrange(patient, time) %>%
        group_by(patient) %>%
        mutate(diff_in_sec = difftime(time, lag(time, default=first(time)), units="secs"),
               diff_in_min = difftime(time, lag(time, default=first(time)), units="mins"),
               diff_in_hour = difftime(time, lag(time, default=first(time)), units="hours"),
               diff_in_days = difftime(time, lag(time, default=first(time)), units="days"),
               month_year = format(time, "%B-%Y")) %>%
        #ungroup() %>%
        #arrange(time)  #this will sort your data on month_year column
      

      编辑:更新代码以将 NA 替换为 0

      【讨论】:

      • @Robert 也许你应该 accept the answer 如果它帮助你解决了你的问题,那么问题可以被认为是关闭
      • 谢谢,我运行了你的代码,但是我的要求有点不同,我需要你的帮助是从我上面的脚本中获取时间列并获取月份和年份并显示在新列中,您的代码中的时差计算包含“NA”。请帮忙。
      • 查看更新后的答案。如果您想对 month_year 列上的数据进行排序,只需取消最后一行的注释即可。
      【解决方案3】:
      library(lubridate)
      library(dplyr)
      library(bupaR)
      
      patients %>% 
        mutate(mon.yr = paste0(month(time, label = T), "-", year(time))) %>% 
        arrange(time)
      

      输出:

      #Event log consisting of:
      #5442 events
      #7 traces
      #500 cases
      #7 activities
      #2721 activity instances 
      #
      ## A tibble: 5,442 x 8
      #   handling              patient employee handling_id registration_type time                .order mon.yr  
      #   <fct>                 <chr>   <fct>    <chr>       <fct>             <dttm>               <int> <chr>   
      # 1 Registration          1       r1       1           start             2017-01-02 11:41:53      1 Jan-2017
      # 2 Registration          2       r1       2           start             2017-01-02 11:41:53      2 Jan-2017
      # 3 Triage and Assessment 1       r2       501         start             2017-01-02 12:40:20    501 Jan-2017
      # 4 Registration          1       r1       1           complete          2017-01-02 12:40:20   2722 Jan-2017
      # 5 Registration          2       r1       2           complete          2017-01-02 15:16:38   2723 Jan-2017
      # 6 Triage and Assessment 2       r2       502         start             2017-01-02 22:32:25    502 Jan-2017
      # 7 Triage and Assessment 1       r2       501         complete          2017-01-02 22:32:25   3222 Jan-2017
      # 8 Triage and Assessment 2       r2       502         complete          2017-01-03 12:34:01   3223 Jan-2017
      # 9 Registration          4       r1       4           start             2017-01-04 01:34:04      4 Jan-2017
      #10 Registration          3       r1       3           start             2017-01-04 01:34:05      3 Jan-2017
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-01-18
        • 2018-06-20
        • 2022-01-05
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多