Reading mdy_hms AM/PM off excel into r

后端 未结 1 1414
傲寒
傲寒 2021-01-29 01:26

I am using dplyr and lubridate.

I am using read_excel to export a data.frame into R from excel. In Excel, I have a column that consists of mdy_hms AM or PM. In R, my cod

1条回答
  •  离开以前
    2021-01-29 01:29

    I would suggest you keep all your timeline data in a date or datetime format as long as possible, to enable calculations and sorting. In my experience, mdy makes more sense as a formatting choice at the last step, since R does not see mdy data as dates at all, but rather as character strings. So you will get unexpected results if you try to calculate anything in mdy or visualize it.

    library(dplyr); library(lubridate)
    df = data_frame(dateTimeEtc = c("2017-03-07 11:10:37 UTC", 
                        "2017-03-22 18:04:42 UTC", 
                        "2017-03-08 09:36:49 UTC"))
    
    df
    #> # A tibble: 3 x 1
    #>   dateTimeEtc            
    #>                     
    #> 1 2017-03-07 11:10:37 UTC
    #> 2 2017-03-22 18:04:42 UTC
    #> 3 2017-03-08 09:36:49 UTC
    
    
    # Convert character to datetime POSIXct (if not already that format),
    #  then make a character string in mdy format and 12 hour clock. 
    df2 <- df %>% 
      mutate(dateTimeEtc2 = ymd_hms(dateTimeEtc),
             as_mdy = format(dateTimeEtc2, '%m/%d/%Y %I:%M:%S %p'))
    df2
    #> # A tibble: 3 x 3
    #>   dateTimeEtc             dateTimeEtc2        as_mdy                
    #>                                                     
    #> 1 2017-03-07 11:10:37 UTC 2017-03-07 11:10:37 03/07/2017 11:10:37 AM
    #> 2 2017-03-22 18:04:42 UTC 2017-03-22 18:04:42 03/22/2017 06:04:42 PM
    #> 3 2017-03-08 09:36:49 UTC 2017-03-08 09:36:49 03/08/2017 09:36:49 AM
    

    0 讨论(0)
提交回复
热议问题