将R时间列转换为特定字符串

将R时间列转换为特定字符串,r,datetime,R,Datetime,我有一列表示R中数据帧中的时间 当我在列上调用str()函数时,它会这样说 >str(df2$Time) Factor w/ 1441 levels "","00:01","00:02","00:03",..: 1086 1096 1111 and so on 我想把这个列转换成字符串类型,如果时间小于12:00,它应该被修改为字符串“moring”,如果时间在12:00到6:00之间,它就是“daylight”,以此类推 我想到的第一步是将这个向量转换为数据帧的时间类型列,所以我使用

我有一列表示R中数据帧中的时间

当我在列上调用str()函数时,它会这样说

>str(df2$Time)
 Factor w/ 1441 levels "","00:01","00:02","00:03",..: 1086 1096 1111 and so on
我想把这个列转换成字符串类型,如果时间小于12:00,它应该被修改为字符串“moring”,如果时间在12:00到6:00之间,它就是“daylight”,以此类推

我想到的第一步是将这个向量转换为数据帧的时间类型列,所以我使用了chron函数

我键入了以下命令

>df2$Time<-chron(times=df2$Time,format=c('h:m'))
 Error in convert.times(times., fmt) : format h:m may be incorrect
 In addition: Warning message:
In is.na(out$s) : is.na() applied to non-(list or vector) of type 'NULL"

>df2$Time第一个可复制的示例:

time <- expand.grid(0:23,0:59)
time <- apply(time,1,function(x)sprintf("%02i:%02i",x[1],x[2]))

lubridate(对不起,Joran,我喜欢这个软件包)和函数
hour
hm
也一样:

Time <- hour(hm("13:24","19:32","3:45","08:25", "21:45", "11:13", "00:00"))
your_breaks <- hour(hm("00:00", "6:00", "12:00", "18:00", "23:59"))
your_labels <- c("Night", "Morning", "Afternoon", "Evening")
cut(x=Time, breaks=your_breaks, labels=your_labels, include.lowest=TRUE)

[1] Afternoon Evening   Night     Morning   Evening   Morning   Night
Time使用chron的
“times”
类和
cut

library(chron)

# data in reproducible form
df2 <- data.frame(Times = c("00:01","12:02","19:03"))

df2$Times <- times(paste0(df2$Times, ":00")) # append a chron "times" class column
breaks <- c(0, 12, 18, 24) / 24 # times are internally fractions of a day
labels <- c("morning", "daylight", "evening")
df2$ind <- cut(df2$Times, breaks, labels, include.lowest = TRUE)
下次请以可复制的形式提供您的数据


修改了次要简化和固定打字错误。

df2$Time
的类型系数为小时:分钟值,如
06:00
。使用
df2$Time@lukeA这是一个很好的解决方案,您应该添加它作为答案。上面的命令将做什么?我的意思是它将如何分割?我想在晚上8点到早上6点=晚上,早上6点到早上8点,下午5:30到晚上8:00作为twighlight,早上8点到下午5:30作为白天分开。在上面的命令中应该执行哪些修改。在参数
breaks
中可以方便地给出中断。因此,您必须通过以下方式对其进行修改:
cut(time,breaks=as.POSIXct(粘贴(“2001-01-01”),c(“00:00:00”,“06:00:00”,“08:00:00”,“17:30:00”,“20:00:00”,“23:59:59”)),labels=c(“夜间”,“黄昏”,“日光”,“黄昏”,“夜间”)
我这样做了,但得到了一个警告:不推荐因子中的重复级别。如果您以“00:00:00”开始break参数,并以“00:00:00”结束,则会发生这种情况。这就是我使用“23:59:59”的原因“。你不能重复一个断点。对不起,我是新来的。可复制形式的确切含义是什么?它意味着它是自包含的,并且是一种R可以直接读取的形式,无需手动操作。i、 e.应该可以将文章中的数据和代码复制到剪贴板,然后将其粘贴到R会话中运行。请参阅和
dput
命令和打字。我已经修好了。
Time <- hour(hm("13:24","19:32","3:45","08:25", "21:45", "11:13", "00:00"))
your_breaks <- hour(hm("00:00", "6:00", "12:00", "18:00", "23:59"))
your_labels <- c("Night", "Morning", "Afternoon", "Evening")
cut(x=Time, breaks=your_breaks, labels=your_labels, include.lowest=TRUE)

[1] Afternoon Evening   Night     Morning   Evening   Morning   Night
library(chron)

# data in reproducible form
df2 <- data.frame(Times = c("00:01","12:02","19:03"))

df2$Times <- times(paste0(df2$Times, ":00")) # append a chron "times" class column
breaks <- c(0, 12, 18, 24) / 24 # times are internally fractions of a day
labels <- c("morning", "daylight", "evening")
df2$ind <- cut(df2$Times, breaks, labels, include.lowest = TRUE)
> df2
     Times      ind
1 00:01:00  morning
2 12:02:00 daylight
3 19:03:00  evening