【问题标题】:Changing datetime format in R using POSIXct-class使用 POSIXct 类更改 R 中的日期时间格式
【发布时间】:2021-11-01 22:02:29
【问题描述】:

我有两个不同格式的日期/时间数据框。我想让 df2 中的 EncounterDate 与 df1 中的格式匹配(例如:“1/1/21 00:00”)。我的日期没有任何时间,因此可以将我的日期的所有时间格式化为 00:00(例如:“1/1/21 将是 1/1/21 00:00”)。

有人告诉我使用 POSIXct 类(时间戳)向量 as.POSIXct(paste0(DateTime,"-00"), format="%m/%d/%Y %H:%M:%S" )。我是 R 新手。如何将它与我的数据框一起使用?

df1 <- structure(list(UserID = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 1L), 
                      Full.Name = c( "John Smith", "Jack Peters", "Bob Brown", "Jane Doe", "Jackie Jane", "Sarah Brown", "Chloe Brown", "John Smith" ), 
                      Info = c("yes", "no", "yes", "yes", "yes", "yes", "no", "yes"), 
                      EncounterID = c(13L, 14L, 15L, 16L, 17L, 18L, 19L, 13L), DateTime = c("1/2/21 00:00", "1/5/21 12:00", "1/1/21 1:31", "1/5/21 3:34", "5/9/21 5:33", "5/8/21 3:39", "12/12/21 2:30", "12/11/21 9:21"), 
                      Temp = c("100", "103", "104", "103", "101", "102", "103", "105"), 
 
                      misc = c("(null)", "no", "(null)", "(null)", "(null)","(null)", "(null)", "(null)" 
                                    )), 
                 class = "data.frame", row.names = c(NA, 
                                                     -8L))

df2 <- structure(list(UserID = c(1L, 2L, 3L, 4L, 5L, 6L), 
                      Full.Name = c("John Smith", "Jack Peters", "Bob Brown", "Jane Doe", "Jackie Jane", "Sarah Brown"), 
                      DOB = c("1/1/90", "1/10/90", "1/2/90", "2/20/80", "2/2/80", "12/2/80"), 
                      EncounterID = c(13L, 14L, 15L, 16L, 17L, 18L), EncounterDate = c("1/1/21", "1/2/21", "1/1/21", "1/6/21", "5/7/21", "5/8/21"), 
                      Type = c("Intro", "Intro", "Intro", "Intro", "Care", "Out"), 
                      responses = c("(null)", "no", 
                                    "yes", "no", "no", "unsat")), 
                      
                 class = "data.frame", row.names = c(NA, 
                                                     -6L))

【问题讨论】:

    标签: r posixct


    【解决方案1】:

    更新:我们可以使用 lubridate 包的 mdyas_datetimefunction:

    library(dplyr)
    library(lubridate)
    df1 %>% 
      separate(DateTime, c("Date", "Time"), sep=" ") %>% 
      mutate(Date = as_datetime(mdy(Date))) %>% 
      select(-Time) %>% 
      as_tibble()
    
    df2 %>% 
      mutate(across(c(DOB, EncounterDate), mdy)) %>% 
      mutate(across(c(DOB, EncounterDate), as_datetime)) %>% 
      as_tibble()
    
     UserID Full.Name   Info  EncounterID Date                Temp  misc  
       <int> <chr>       <chr>       <int> <dttm>              <chr> <chr> 
    1      1 John Smith  yes            13 2021-01-02 00:00:00 100   (null)
    2      2 Jack Peters no             14 2021-01-05 00:00:00 103   no    
    3      3 Bob Brown   yes            15 2021-01-01 00:00:00 104   (null)
    4      4 Jane Doe    yes            16 2021-01-05 00:00:00 103   (null)
    5      5 Jackie Jane yes            17 2021-05-09 00:00:00 101   (null)
    6      6 Sarah Brown yes            18 2021-05-08 00:00:00 102   (null)
    7      7 Chloe Brown no             19 2021-12-12 00:00:00 103   (null)
    8      1 John Smith  yes            13 2021-12-11 00:00:00 105   (null)
    > 
    
      UserID Full.Name   DOB                 EncounterID EncounterDate       Type 
       <int> <chr>       <dttm>                    <int> <dttm>              <chr>
    1      1 John Smith  1990-01-01 00:00:00          13 2021-01-01 00:00:00 Intro
    2      2 Jack Peters 1990-01-10 00:00:00          14 2021-01-02 00:00:00 Intro
    3      3 Bob Brown   1990-01-02 00:00:00          15 2021-01-01 00:00:00 Intro
    4      4 Jane Doe    1980-02-20 00:00:00          16 2021-01-06 00:00:00 Intro
    5      5 Jackie Jane 1980-02-02 00:00:00          17 2021-05-07 00:00:00 Care 
    6      6 Sarah Brown 1980-12-02 00:00:00          18 2021-05-08 00:00:00 Out  
    

    【讨论】:

    • 感谢发帖。好像每次都删了?我知道我没有 df2 的时间,但我想要 df2 的时间,即使我们必须将它们默认为 00:00
    • 请看我的更新!:-)
    • 谢谢!我可以在控制台中看到更改,但在环境中双击 df2 时看不到更改。关于如何查看环境变化的任何想法?
    • df1 &lt;-放在df1之前
    • 我试过了。它仍然不会出现。我什至尝试使用不同的名称,例如 df11 &lt;- 而不是 df1,但我仍然看不到新结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-21
    • 2020-12-07
    • 2013-04-24
    • 2021-12-22
    • 2016-10-20
    相关资源
    最近更新 更多