Create date column from datetime in R

后端 未结 2 1170
走了就别回头了
走了就别回头了 2021-01-29 15:35

I am new to R and I am an avid SAS programmer and am just having a difficult time wrapping my head around R.

Within a data frame I have a date time column formatted as

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-29 16:35

    It is wise to always to be careful with as.Date(as.POSIXct(...)):

    E.g., for me in Australia:

    df <- data.frame(dt=as.POSIXct("2013-01-01 00:53:00"))
    df
    #                   dt
    #1 2013-01-01 00:53:00
    
    as.Date(df$dt)
    #[1] "2012-12-31"
    

    You'll see that this is problematic as the dates don't match. You'll hit problems if your POSIXct object is not in the UTC timezone as as.Date defaults to tz="UTC" for this class. See here for more info: as.Date(as.POSIXct()) gives the wrong date?
    To be safe you probably need to match your timezones:

    as.Date(df$dt,tz=Sys.timezone()) #assuming you've just created df in the same session:
    #[1] "2013-01-01"
    

    Or safer option #1:

    df <- data.frame(dt=as.POSIXct("2013-01-01 00:53:00",tz="UTC"))
    as.Date(df$dt)
    #[1] "2013-01-01"
    

    Or safer option #2:

    as.Date(df$dt,tz=attr(df$dt,"tzone"))
    #[1] "2013-01-01"
    

    Or alternatively use format to extract parts of the POSIXct object:

    as.Date(format(df$dt,"%Y-%m-%d"))
    #[1] "2013-01-01"
    as.numeric(format(df$dt,"%Y"))
    #[1] 2013
    as.numeric(format(df$dt,"%m"))
    #[1] 1
    as.numeric(format(df$dt,"%d"))
    #[1] 1
    

提交回复
热议问题