Home > database >  Reverse the order between year and days in a datetime vector
Reverse the order between year and days in a datetime vector

Time:09-14

I have some raw data

po59<-structure(list(case_id = c("WC4120721", "WC4120667", "WC4120689", 
"WC4121068", "WC4120667", "WC4120666", "WC4120667", "WC4121068", 
"WC4120667", "WC4121068", "WC4121068", "WC4120666", "WC4120721", 
"WC4120666", "WC4120666"), lifecycle = c(110, 110, 110, 110, 
120, 110, 130, 120, 10, 130, 10, 120, 120, 130, 10), action = c("WC4120721-CN354877", 
"WC4120667-CN354878", "WC4120689-CN356752", "WC4121068-CN301950", 
"WC4120667-CSW310", "WC4120666-CN354878", "WC4120667-CSW308", 
"WC4121068-CSW303", "WC4120667-CSW309", "WC4121068-CSW308", "WC4121068-CSW309", 
"WC4120666-CSW310", "WC4120721-CSW310", "WC4120666-CSW308", "WC4120666-CSW309"
), activity = c("Forged Wire, Medium (Sport)", "Forged Wire, Medium (Sport)", 
"Forged Wire, Medium (Sport)", "Forged Wire, Medium (Sport)", 
"BBH-1&2", "Forged Wire, Medium (Sport)", "TCE Cleaning", "SOLO Oil", 
"Tempering", "TCE Cleaning", "Tempering", "BBH-1&2", "BBH-1&2", 
"TCE Cleaning", "Tempering"), resource = c("3419", "3216", "3409", 
"3201", "C3-100", "3216", "C3-080", "C3-030", "C3-090", "C3-080", 
"C3-090", "C3-100", "C3-100", "C3-080", "C3-090"), timestamp = c("03.12.20 6:00", 
"04.12.20 23:58", "08.12.20 16:56", "10.12.20 5:42", "10.12.20 22:57", 
"11.12.20 10:13", "11.12.20 14:17", "11.12.20 21:20", "12.12.20 2:32", 
"12.12.20 6:35", "12.12.20 14:50", "14.12.20 13:50", "14.12.20 19:11", 
"15.12.20 1:10", "15.12.20 9:25")), row.names = c(NA, -15L), class = c("tbl_df", 
"tbl", "data.frame"))

Then I convert to bupaR object with:

po59br<-bupaR::eventlog(eventlog = po59,
                       case_id = "case_id",
                       activity_id = "activity",
                       activity_instance_id = "action",
                       lifecycle_id = "lifecycle",
                       timestamp = "timestamp",
                       resource_id = "resource"
 )

and I try to give the datetime data a format like : 2020-12-03 06:00:00 instead of 0003-12-20 06:00:00

po59$timestamp<-as.POSIXct(po59$timestamp,format="%Y.%m.%d 
%H:%M",tz=Sys.timezone())

CodePudding user response:

Before and after:

po59$timestamp
#  [1] "03.12.20 6:00"  "04.12.20 23:58" "08.12.20 16:56" "10.12.20 5:42"  "10.12.20 22:57" "11.12.20 10:13" "11.12.20 14:17"
#  [8] "11.12.20 21:20" "12.12.20 2:32"  "12.12.20 6:35"  "12.12.20 14:50" "14.12.20 13:50" "14.12.20 19:11" "15.12.20 1:10" 
# [15] "15.12.20 9:25" 

as.POSIXct(po59$timestamp, format="%d.%m.%y %H:%M")
#  [1] "2020-12-03 06:00:00 EST" "2020-12-04 23:58:00 EST" "2020-12-08 16:56:00 EST" "2020-12-10 05:42:00 EST"
#  [5] "2020-12-10 22:57:00 EST" "2020-12-11 10:13:00 EST" "2020-12-11 14:17:00 EST" "2020-12-11 21:20:00 EST"
#  [9] "2020-12-12 02:32:00 EST" "2020-12-12 06:35:00 EST" "2020-12-12 14:50:00 EST" "2020-12-14 13:50:00 EST"
# [13] "2020-12-14 19:11:00 EST" "2020-12-15 01:10:00 EST" "2020-12-15 09:25:00 EST"

This is what should be done when you first read in and process this data, in place of any other conversion you have on timestamp:

po59$timestamp <- as.POSIXct(po59$timestamp, format="%d.%m.%y %H:%M")
po59
# # A tibble: 15 x 6
#    case_id   lifecycle action             activity                    resource timestamp               
#    <chr>         <dbl> <chr>              <chr>                       <chr>    <dttm>                  
#  1 WC4120721       110 WC4120721-CN354877 Forged Wire, Medium (Sport) 3419     2020-12-03 06:00:00.0000
#  2 WC4120667       110 WC4120667-CN354878 Forged Wire, Medium (Sport) 3216     2020-12-04 23:58:00.0000
#  3 WC4120689       110 WC4120689-CN356752 Forged Wire, Medium (Sport) 3409     2020-12-08 16:56:00.0000
#  4 WC4121068       110 WC4121068-CN301950 Forged Wire, Medium (Sport) 3201     2020-12-10 05:42:00.0000
#  5 WC4120667       120 WC4120667-CSW310   BBH-1&2                     C3-100   2020-12-10 22:57:00.0000
#  6 WC4120666       110 WC4120666-CN354878 Forged Wire, Medium (Sport) 3216     2020-12-11 10:13:00.0000
#  7 WC4120667       130 WC4120667-CSW308   TCE Cleaning                C3-080   2020-12-11 14:17:00.0000
#  8 WC4121068       120 WC4121068-CSW303   SOLO Oil                    C3-030   2020-12-11 21:20:00.0000
#  9 WC4120667        10 WC4120667-CSW309   Tempering                   C3-090   2020-12-12 02:32:00.0000
# 10 WC4121068       130 WC4121068-CSW308   TCE Cleaning                C3-080   2020-12-12 06:35:00.0000
# 11 WC4121068        10 WC4121068-CSW309   Tempering                   C3-090   2020-12-12 14:50:00.0000
# 12 WC4120666       120 WC4120666-CSW310   BBH-1&2                     C3-100   2020-12-14 13:50:00.0000
# 13 WC4120721       120 WC4120721-CSW310   BBH-1&2                     C3-100   2020-12-14 19:11:00.0000
# 14 WC4120666       130 WC4120666-CSW308   TCE Cleaning                C3-080   2020-12-15 01:10:00.0000
# 15 WC4120666        10 WC4120666-CSW309   Tempering                   C3-090   2020-12-15 09:25:00.0000

Data

po59 <- structure(list(case_id = c("WC4120721", "WC4120667", "WC4120689", "WC4121068", "WC4120667", "WC4120666", "WC4120667", "WC4121068", "WC4120667", "WC4121068", "WC4121068", "WC4120666", "WC4120721", "WC4120666", "WC4120666"), lifecycle = c(110, 110, 110, 110, 120, 110, 130, 120, 10, 130, 10, 120, 120, 130, 10), action = c("WC4120721-CN354877", "WC4120667-CN354878", "WC4120689-CN356752", "WC4121068-CN301950", "WC4120667-CSW310", "WC4120666-CN354878", "WC4120667-CSW308", "WC4121068-CSW303", "WC4120667-CSW309",  "WC4121068-CSW308", "WC4121068-CSW309", "WC4120666-CSW310", "WC4120721-CSW310", "WC4120666-CSW308", "WC4120666-CSW309"), activity = c("Forged Wire, Medium (Sport)", "Forged Wire, Medium (Sport)", "Forged Wire, Medium (Sport)", "Forged Wire, Medium (Sport)", "BBH-1&2", "Forged Wire, Medium (Sport)", "TCE Cleaning", "SOLO Oil", "Tempering", "TCE Cleaning", "Tempering", "BBH-1&2", "BBH-1&2", "TCE Cleaning", "Tempering"), resource = c("3419", "3216", "3409", "3201", "C3-100", "3216", "C3-080", "C3-030",  "C3-090", "C3-080", "C3-090", "C3-100", "C3-100", "C3-080", "C3-090"), timestamp = c("03.12.20 6:00", "04.12.20 23:58", "08.12.20 16:56", "10.12.20 5:42", "10.12.20 22:57", "11.12.20 10:13", "11.12.20 14:17", "11.12.20 21:20", "12.12.20 2:32", "12.12.20 6:35", "12.12.20 14:50", "14.12.20 13:50", "14.12.20 19:11", "15.12.20 1:10", "15.12.20 9:25")), row.names = c(NA, -15L), class = c("tbl_df", "tbl", "data.frame"))

CodePudding user response:

You can use the library lubridate to fix your dates.

Starting with the data structured exactly as it is in your question, update them by switching the year and date. (Add the 2K back to the year, as well.)

library(lubridate)

po59$timestamp <- update(po59$timestamp,
                         year = 2000   day(po59$timestamp),
                         day = year(po59$timestamp))
head(po59)
# # A tibble: 6 × 6
#   case_id   lifecycle action             activity    resou…¹ timestamp          
#   <chr>         <dbl> <chr>              <chr>       <chr>   <dttm>             
# 1 WC4120721       110 WC4120721-CN354877 Forged Wir… 3419    2020-12-03 06:00:00
# 2 WC4120667       110 WC4120667-CN354878 Forged Wir… 3216    2020-12-04 23:58:00
# 3 WC4120689       110 WC4120689-CN356752 Forged Wir… 3409    2020-12-08 16:56:00
# 4 WC4121068       110 WC4121068-CN301950 Forged Wir… 3201    2020-12-10 05:42:00
# 5 WC4120667       120 WC4120667-CSW310   BBH-1&2     C3-100  2020-12-10 22:57:00
# 6 WC4120666       110 WC4120666-CN354878 Forged Wir… 3216    2020-12-11 10:13:00
  • Related