Create date column from datetime in R

为君一笑 提交于 2021-02-05 12:32:33

问题


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 a POSIXct with the following the column appearing as "2013-01-01 00:53:00". I would like to create a date column using a function that extracts the date and a column to extract the hour. In an ideal world I would like to be able to extract the date, year, day, month, time and hour all within the data frame to create these additional columns within the data frame.


回答1:


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



回答2:


Use the lubridate package. For example, if df is a data.frame with a column dt of type POSIXct, then you could:

df$date = as.Date(as.POSIXct(df$dt, tz="UTC"))
df$year = year(df$dt)
df$month = month(df$dt)
df$day = day(df$dt)
# and so on...

If your can store your data in a data.table, then this is even easier:

df[, `:=`(date = as.Date(as.POSIXct(dt, tz="UTC")), year = year(dt), ...)]


来源:https://stackoverflow.com/questions/30653521/create-date-column-from-datetime-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!