R:将数据框的列设置为日期时间以创建间隔 [英] R: set a column of a dataframe as date-time in order to create intervals
本文介绍了R:将数据框的列设置为日期时间以创建间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为data的数据框,该数据框在列中具有日期时间信息,格式为:mm/dd/yyyy hh:mm:ss am/pm
I have a dataframe called data that has date-time information in a column, in the following format: mm/dd/yyyy hh:mm:ss am/pm
我想要的是创建放置时间值:1小时内,2小时内等.
What I want is to create put timeperiod values: within 1 hour, within 2 hours etc.
数据框的几行:
time timeperiod refer x.x
1 05/07/2017 18:00:15 NA 1 23.97370
2 06/08/2017 06:21:12 NA 1 23.79394
3 06/08/2017 08:03:11 NA 1 23.79394
4 06/08/2017 08:12:47 NA 1 23.79394
5 07/07/2017 10:41:45 NA 1 23.54257
6 1/21/2017 8:10:20 AM NA 1 21.06460
7 1/9/2013 10:01:32 PM NA 2 23.99733
8 1/9/2013 10:01:32 PM NA 2 23.99733
9 1/9/2013 10:01:32 PM NA 2 21.06460
10 1/9/2013 10:01:32 PM NA 2 21.06460
我尝试过: strftime(data $ time.,format =%m/%d/%y%H:%M:%S"%r),但我收到以下错误:
I tried: strftime(data$time., format = "%m/%d/%y %H:%M:%S" %r) but I am receiving the following error:
as.POSIXlt.character(as.character(x),...)中的错误: 字符串不是标准的明确格式
Error in as.POSIXlt.character(as.character(x), ...) : character string is not in a standard unambiguous format
推荐答案
一个选项是
i1 <- grepl("AM|PM", df1$time)
out <- as.POSIXct(rep(NA_real_, nrow(df1)), origin = "1970-01-01")
out[i1] <- as.POSIXct(df1$time[i1], format = "%m/%d/%Y %I:%M:%S %p")
out[!i1] <- as.POSIXct(df1$time[!i1], format = '%m/%d/%Y %H:%M:%S')
droplevels(cut(out, breaks = "1 hour"))
#[1] 2017-05-07 18:00:00 2017-06-08 06:00:00 2017-06-08 08:00:00 2017-06-08 08:00:00 2017-07-07 10:00:00 2017-01-21 08:00:00
#[7] 2013-01-09 22:00:00 2013-01-09 22:00:00 2013-01-09 22:00:00 2013-01-09 22:00:00
#Levels: 2013-01-09 22:00:00 2017-01-21 08:00:00 2017-05-07 18:00:00 2017-06-08 06:00:00 2017-06-08 08:00:00 2017-07-07 10:00:00
或者另一个选择是parsedate
和floor_date
library(lubridate)
library(parsedate)
floor_date(parse_date(df1$time), "hour")
#[1] "2017-05-07 18:00:00 UTC" "2017-06-08 06:00:00 UTC" "2017-06-08 08:00:00 UTC" "2017-06-08 08:00:00 UTC" "2017-07-07 10:00:00 UTC"
#[6] "2017-01-21 08:00:00 UTC" "2013-01-09 22:00:00 UTC" "2013-01-09 22:00:00 UTC" "2013-01-09 22:00:00 UTC" "2013-01-09 22:00:00 UTC"
数据
df1 <- structure(list(time = c("05/07/2017 18:00:15", "06/08/2017 06:21:12",
"06/08/2017 08:03:11", "06/08/2017 08:12:47", "07/07/2017 10:41:45",
"1/21/2017 8:10:20 AM", "1/9/2013 10:01:32 PM", "1/9/2013 10:01:32 PM",
"1/9/2013 10:01:32 PM", "1/9/2013 10:01:32 PM"), timeperiod = c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA), refer = c(1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L), x.x = c(23.9737, 23.79394, 23.79394,
23.79394, 23.54257, 21.0646, 23.99733, 23.99733, 21.0646, 21.0646
)), class = "data.frame", row.names = c("1", "2", "3", "4", "5",
"6", "7", "8", "9", "10"))
这篇关于R:将数据框的列设置为日期时间以创建间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文