Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何更改按时段分类的日期时间格式_R_Datetime - Fatal编程技术网

R 如何更改按时段分类的日期时间格式

R 如何更改按时段分类的日期时间格式,r,datetime,R,Datetime,我需要你的帮助来解决我在R的datetime问题 我在factor类中有一个datetime,如: "02/02/2010 12:00" "02/02/2010 12:05" "02/02/2010 12:10" 然后我只想选择时间,比如:“12:00”“12:05”“12:10” 我试着先把factor转换成character,然后再把它改成date类。我的目标是将时间范围划分为时段(晚上、中午、下午、晚上) getPeriode我经常发现date和time类上的操作非常混乱,在这种情况下,

我需要你的帮助来解决我在R的datetime问题

我在factor类中有一个datetime,如:

"02/02/2010 12:00" "02/02/2010 12:05" "02/02/2010 12:10"
然后我只想选择时间,比如:
“12:00”“12:05”“12:10”

我试着先把factor转换成character,然后再把它改成date类。我的目标是将时间范围划分为时段(晚上、中午、下午、晚上)


getPeriode我经常发现date和time类上的操作非常混乱,在这种情况下,我认为将变量作为字符串和整数使用会更容易。我不确定我是否理解了您的所有代码,但这可能会激发您自己的解决方案

datetime <- c("02/02/2010 07:00", "02/02/2010 08:05", "02/02/2010 11:00",
              "02/02/2010 12:10", "02/02/2010 16:10", "02/02/2010 23:10")
heure <- as.integer(substr(datetime, 12, 13))
conversion <- data.frame(datetime=datetime, heure=heure,
     period=cut(heure, c(-Inf, 7, 10, 12, 17, Inf),
                labels=c("night", "morning", "noon", "afternoon", "evening")))

您也可以将其格式设置为一小时,而不是使用
substr
dates=as.POSIXlt(dates,format='%d/%m/%Y%H:%m')
后跟
heure=as.numeric(format(dates,'%H'))
就行了。@Backlin:非常感谢您的回答,这给了我解决方案的想法。:)诺格拉普斯:是的,你是对的,它的工作与你的方法。。谢谢
d <- as.character.POSIXt(HEURE, format='%H:%M:%S')
datetime <- c("02/02/2010 07:00", "02/02/2010 08:05", "02/02/2010 11:00",
              "02/02/2010 12:10", "02/02/2010 16:10", "02/02/2010 23:10")
heure <- as.integer(substr(datetime, 12, 13))
conversion <- data.frame(datetime=datetime, heure=heure,
     period=cut(heure, c(-Inf, 7, 10, 12, 17, Inf),
                labels=c("night", "morning", "noon", "afternoon", "evening")))
> conversion
          datetime heure    period
1 02/02/2010 07:00     7     night
2 02/02/2010 08:05     8   morning
3 02/02/2010 11:00    11      noon
4 02/02/2010 12:10    12      noon
5 02/02/2010 16:10    16 afternoon
6 02/02/2010 23:10    23   evening