我有一个数据库,其中包含用于以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'
有人可以帮我解决问题,如何提取时间,日期等?
答案 0 :(得分:2)
我们使用dplyr
时的一个选项是转换为POSIXct
(因为不支持POSIXlt
类)并使用{{1}提取hour
}。
lubridate
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
答案 1 :(得分:1)
我首先建议将您的created_utc
转换为POSIXct
类(而不是POSIXlt
,然后提取您需要的所有数据。以下是使用{{1}的简单说明包
data.table
答案 2 :(得分:1)
所有这些都可以用基础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>