将日期时间转换为计数?

时间:2014-10-24 13:02:56

标签: r

我想将日期转换为日期计数,将时间转换为小时数。我能够将日期时间转换为日期时间:

           data$date <- as.Date(data$datetime)
           data$time <- format(data$datetime,"%H:%M:%S")
           data$datetime<-strptime(as.character(data$datetime),
                                               "%Y-%m-%d %H:%M:%S")

然而不知道如何继续。我想得到以下结果:

                 date         time       day_count       hours

                2011-01-01  00:00:00         1             0
                2011-01-01  01:00:00         1             1
                2011-01-01  02:00:00         1             2 
                2011-01-01  03:00:00         1             3
                …           … 
                2011-01-02  23:00:00         1            23
                2011-01-02  00:00:00         2             0
                2011-01-02  01:00:00         2             1
                2011-01-02  02:00:00         2             2
                2011-01-02  03:00:00         2             3
                …           … 

谢谢

3 个答案:

答案 0 :(得分:1)

您可以通过

base R中执行此操作
  data$day_count <- as.numeric(format(data$date, "%d"))
  data$day_count
  #[1] 1 1 1 1 2 2 2 2 2

  data$hours <- as.numeric(format(data$datetime, "%H"))
  data$hours
  #[1]  0  1  2  3 23  0  1  2  3

更新

如果你想连续计数长达一年

  as.numeric(format(data$date, "%j")) #will count up to 366 days and then restart
  #[1] 1 1 1 1 2 2 2 2 2

    date1 <-seq(as.Date("2001-01-01"), length.out=415, by='1 day')
    Count1 <- as.numeric(format(date1, "%j"))

或者您可以每月修改重新启动的计数。假设v1是您的day_count

  v1 <- rep(c(rep(1:31, each=5), rep(1:30, each=4)),2)
  indx <- cumsum(c(TRUE,diff(v1)<0))
  indx1 <- tapply(v1, cumsum(c(TRUE,diff(v1)<0)), max)
  v2 <- unname(v1+rep( c(0,cumsum(indx1[-1])), table(indx)))

数据

  data <- structure(list(date = structure(c(14975, 14975, 14975, 14975, 
  14976, 14976, 14976, 14976, 14976), class = "Date"), time = c("00:00:00", 
  "01:00:00", "02:00:00", "03:00:00", "23:00:00", "00:00:00", "01:00:00", 
  "02:00:00", "03:00:00"), datetime = structure(list(sec = c(0, 
  0, 0, 0, 0, 0, 0, 0, 0), min = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
 0L, 0L), hour = c(0L, 1L, 2L, 3L, 23L, 0L, 1L, 2L, 3L), mday = c(1L, 
 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), mon = c(0L, 0L, 0L, 0L, 0L, 
 0L, 0L, 0L, 0L), year = c(111L, 111L, 111L, 111L, 111L, 111L, 
 111L, 111L, 111L), wday = c(6L, 6L, 6L, 6L, 0L, 0L, 0L, 0L, 0L
 ), yday = c(0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L), isdst = c(0L, 
 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), zone = c("EST", "EST", "EST", 
"EST", "EST", "EST", "EST", "EST", "EST"), gmtoff = c(NA_integer_, 
 NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
 NA_integer_, NA_integer_, NA_integer_)), .Names = c("sec", "min", 
"hour", "mday", "mon", "year", "wday", "yday", "isdst", "zone", 
 "gmtoff"), class = c("POSIXlt", "POSIXt"))), .Names = c("date", 
"time", "datetime"), class = "data.frame", row.names = c(NA, 
-9L))

答案 1 :(得分:0)

我建议您使用Lubridate库来处理数据时对象,它使一切变得更加容易。我也将使用dplyr库来处理数据帧

library(lubridate)
library(dplyr)

date <- c("2011-01-01", "2011-02-07", "2014-09-11")
time <- c("00:05:00", "12:42:00", "19:11:00")

df <- tbl_df(data.frame(date= ymd(date), time= hms(time)))
df <- mutate(df, day_count= day(date), hours= hour(time))

输出:

> df

Source: local data frame [3 x 4]

        date       time day_count hours
1 2011-01-01      5M 0S         1     0
2 2011-02-07 12H 42M 0S         7    12
3 2014-09-11 19H 11M 0S        11    19

答案 2 :(得分:0)

获取小时(0-23):

 mydate = strptime('2011-01-02  23:00:00',format='%H')

获取星期几的数字(0-6,星期日为0):

mydate = strptime('2011-01-02  23:00:00',format='%w') 

参考文献:

Quick-R

Berkeley