obtain hour from DateTime vector

2019-02-09 02:23发布

I have a DateTime vector within a data.frame where the data frame is made up of 8760 observations representing hourly intervals throughout the year e.g.

2010-01-01 00:00
2010-01-01 01:00
2010-01-01 02:00
2010-01-01 03:00

and so on.

I would like to create a data.frame which has the original DateTime vector as the first column and then the hourly values in the second column e.g.

2010-01-01 00:00          00:00
2010-01-01 01:00          01:00

How can this be achieved?

标签: r date time
4条回答
成全新的幸福
2楼-- · 2019-02-09 02:54

On a more general note, for anyone that comes here from google and maybe wants to group by hour:

The key here is: lubridate::hour(datetime)

p22 in the cran doc here: https://cran.r-project.org/web/packages/lubridate/lubridate.pdf

查看更多
Juvenile、少年°
3楼-- · 2019-02-09 03:05

Or generically, not treating them as dates, you can use the following provided that the time and dates are padded correctly.

library(stringr)
df <- data.frame(DateTime = c("2010-01-01 00:00", "2010-01-01 01:00", "2010-01-01 02:00", "2010-01-01 03:00"))
df <- data.frame(df, Time = str_sub(df$DateTime, -5, -1))

It depends on your needs really.

查看更多
甜甜的少女心
4楼-- · 2019-02-09 03:07

Using lubridate

library(stringr)
library(lubridate)
library(plyr)

df <- data.frame(DateTime = c("2010-01-01 00:00", "2010-01-01 01:00", "2010-01-01 02:00", "2010-01-01 03:00"))

df <- mutate(df, DateTime = ymd_hm(DateTime),
                 time  = str_c(hour(DateTime), str_pad(minute(DateTime), 2, side = 'right', pad = '0'), sep = ':'))
查看更多
We Are One
5楼-- · 2019-02-09 03:16

Use format or strptime to extract the time information.

Create a POSIXct vector:

x <- seq(as.POSIXct("2012-05-21"), by=("+1 hour"), length.out=5)

Extract the time:

data.frame(
  date=x,
  time=format(x, "%H:%M")
)

                 date  time
1 2012-05-21 00:00:00 00:00
2 2012-05-21 01:00:00 01:00
3 2012-05-21 02:00:00 02:00
4 2012-05-21 03:00:00 03:00
5 2012-05-21 04:00:00 04:00

If the input vector is a character vector, then you have to convert to POSIXct first:

Create some data

dat <- data.frame(
  DateTime=format(seq(as.POSIXct("2012-05-21"), by=("+1 hour"), length.out=5), format="%Y-%m-%d %H:%M")
)
dat
          DateTime
1 2012-05-21 00:00
2 2012-05-21 01:00
3 2012-05-21 02:00
4 2012-05-21 03:00
5 2012-05-21 04:00

Split time out:

data.frame(
  DateTime=dat$DateTime,
  time=format(as.POSIXct(dat$DateTime, format="%Y-%m-%d %H:%M"), format="%H:%M")
)

          DateTime  time
1 2012-05-21 00:00 00:00
2 2012-05-21 01:00 01:00
3 2012-05-21 02:00 02:00
4 2012-05-21 03:00 03:00
5 2012-05-21 04:00 04:00
查看更多
登录 后发表回答