【问题标题】:To extract date, hour and weekday from a UTC timestamp -R从 UTC 时间戳中提取日期、小时和工作日 -R
【发布时间】:2015-09-07 15:06:57
【问题描述】:

我有一个数据库,其中有一列以 UTC 表示创建时间,例如

created_utc
1 1430438400
2 1430438410
3 1430438430
4 1430438455
5 1430438470
6 1430438480

我想将日期、小时和 is.weekend 提取到单独的列中。我试过了,

db_subset %>% mutate(hour = as.POSIXlt(created_utc, origin ='1970-01-01')$hour)

但它无法识别created_utc 对象。 我尝试将其强制转换为数据框,然后,

df_comments <- db_subset %>% 
                select(created_utc) %>%
                        collect() %>%
                            data.frame() %>%
                               mutate(hour = as.POSIXlt(created_utc, origin ='1970-01-01')$hour)

但它失败并出现错误:invalid subscript type 'closure'

谁能帮助我在哪里拧,我如何提取时间、日期等?

【问题讨论】:

  • 我认为POSIXlt 类在mutate 中不受支持,您可以尝试使用POSIXct。您可以在%&gt;% 之外执行此操作,即db_subset$hour &lt;- as.POSIXlt(db_subset$created_utc , origin='1970-01-01')$hour

标签: r dplyr unix-timestamp lubridate


【解决方案1】:

如果我们使用dplyr,一个选项是转换为POSIXct(因为不支持POSIXlt 类)并使用lubridate 提取hour

library(lubridate)
library(dplyr)
db_subset %>%
    mutate(hour=hour(as.POSIXct(created_utc, origin='1970-01-01')))
#   created_utc hour
#1  1430438400   20
#2  1430438410   20
#3  1430438430   20
#4  1430438455   20
#5  1430438470   20
#6  1430438480   20

数据

db_subset <- structure(list(created_utc = c(1430438400L, 1430438410L, 
1430438430L, 
1430438455L, 1430438470L, 1430438480L)), .Names = "created_utc", 
class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6"))

【讨论】:

    【解决方案2】:

    我首先建议将您的created_utc 转换为POSIXct 类(而不是POSIXlt,然后提取您需要的所有数据。这是使用data.table 包的简单说明

    library(data.table)
    setDT(df)[, created_utc := as.POSIXct(created_utc, origin = '1970-01-01')]
    df[, `:=`(Date = as.Date(created_utc),
              Hour = hour(created_utc),
              isWeekend = wday(created_utc) %in% c(7L, 1L))]
    df
    #            created_utc       Date Hour isWeekend
    # 1: 2015-05-01 03:00:00 2015-05-01    3     FALSE
    # 2: 2015-05-01 03:00:10 2015-05-01    3     FALSE
    # 3: 2015-05-01 03:00:30 2015-05-01    3     FALSE
    # 4: 2015-05-01 03:00:55 2015-05-01    3     FALSE
    # 5: 2015-05-01 03:01:10 2015-05-01    3     FALSE
    # 6: 2015-05-01 03:01:20 2015-05-01    3     FALSE
    

    【讨论】:

    • 您在示例中使用的是相同的数据吗?我得到不同的“小时”值
    • 可能是时区问题,不知道。
    • 如果我指定tz='UTC,我得到全0
    【解决方案3】:

    所有这些都可以通过基础 R 来完成:

    R> df <- data.frame(created_utc=c(1430438400, 1430438410, 1430438430,
    +                                 1430438455, 1430438470, 1430438480))
    R> df
      created_utc
    1  1430438400
    2  1430438410
    3  1430438430
    4  1430438455
    5  1430438470
    6  1430438480
    R> 
    R> # so far so good -- we just have the data
    R> # so let's make it a date time object 
    R> 
    R> df[,1] <- as.POSIXct(df[,1], origin="1970-01-01")
    R> df
              created_utc
    1 2015-04-30 19:00:00
    2 2015-04-30 19:00:10
    3 2015-04-30 19:00:30
    4 2015-04-30 19:00:55
    5 2015-04-30 19:01:10
    6 2015-04-30 19:01:20
    R> 
    R> ## we can use this to extract Date, Hour and Weekend computations
    R> 
    R> df[,"date"] <- as.Date(df[,1])
    R> df[,"hour"] <- as.POSIXlt(df[,1])$hour
    R> df[,"isWeekend"] <- as.POSIXlt(df[,1])$wday < 1 || as.POSIXlt(df[,1])$wday > 5
    R> df
              created_utc       date hour isWeekend
    1 2015-04-30 19:00:00 2015-05-01   19     FALSE
    2 2015-04-30 19:00:10 2015-05-01   19     FALSE
    3 2015-04-30 19:00:30 2015-05-01   19     FALSE
    4 2015-04-30 19:00:55 2015-05-01   19     FALSE
    5 2015-04-30 19:01:10 2015-05-01   19     FALSE
    6 2015-04-30 19:01:20 2015-05-01   19     FALSE
    R> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 2015-04-14
      • 2017-02-01
      • 2014-11-20
      • 2020-06-27
      • 1970-01-01
      相关资源
      最近更新 更多