将时间值转换为数字,同时保留时间特征 [英] Convert time values to numeric while keeping time characteristics

查看:107
本文介绍了将时间值转换为数字,同时保留时间特征的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,其中包含发生不同事件的间隔时间。我想做的是将数据转换为数值向量,因此在保持其时间特性的同时,更易于操作和运行摘要/制作图形等。这是我的数据的摘要:

I have a data set which contains interval times of different events occurring. What I want to do, is convert the data into a numeric vector, so its easier to manipulate and run summaries/make graphs etc, while keeping its time characteristics. Here is a snippet of my data:

data <- c( "03:31", "12:17", "16:29", "09:52", "04:01", "09:00", "06:29",
           "04:17", "04:42")
class(data)
[1] character

显而易见的答案是:

as.numeric(data)

但我收到此错误:

Warning message:
NAs introduced by coercion 

我想也许把':'取出来了,但是却失去了时间特性。就是说,我的意思是,如果我将347和543的值相加,那么我得到的是890,而不是930。这是我用来删除冒号的代码,可以很好地达到目的:

I thought of maybe taking the ':' out, but then it loses its time characteristics. By that, I mean that if I sum values together say 347 and 543, it would give me 890 as opposed to 930. Here is the code that I would use to take the colon out, which works fine for its purpose:

Nocolon <- gsub("[:]", "", Data, perl=TRUE)
"0331" "1217" "1629" "0952" "0401" "0900" "0629" "0417" "0442"

所以从本质上讲,我想要的是时间值采用易于操作和分析的形式。我的想法是将其作为数值向量,但这是基于我对R的最低了解。我的实际代码具有数千个时间值,并且我想创建一个绘图,以允许我查看并确定值是否遵循a统计分布。

So essentially, what I want is for my time values to be in a form which is easy to manipulate and analyse. My idea is for it to be a numeric vector, but that is from my minimal understanding of R. My actual code has thousands of time values, and I want to create a plot that will allow me to view and determine whether the values follow a statistical distribution.

谢谢!

推荐答案

这里有一些方法。全部转换为分钟。例如,第一个部分是 03:31 ,它是3 * 60 + 31 = 211分钟。 (1)到(5)不要使用任何软件包。

Here are some approaches. All convert to minutes. For example, the first component is "03:31" which is 3 * 60 + 31 = 211 minutes. (1) to (5) do not use any packages.

1)%*%通过阅读数据转换为带有小时和分钟的2列数据框。它将转换为矩阵,以便可以将矩阵乘以 c(60,1)。最后,使用 c 对其进行拆解。

1) %*% It works by reading data into a 2 column data frame with hours and minutes. That is converted to a matrix so that it can be matrix multiplied by c(60, 1). Finally, unravel it with c.

c(as.matrix(read.table(text = data, sep = ":")) %*% c(60, 1))
[1] 211 737 989 592 241 540 389 257 282

2)with 。这种变化甚至更短。它创建了相同的数据框,但是然后将第一列( V1 )乘以60,然后将其添加到第二列( V2 )。

2) with This variation is even shorter. It creates the same data frame but and then simply mulitiplies the first column (V1) by 60 and adds it to the second column (V2).

with(read.table(text = data, sep = ":"), 60*V1+V2)
[1] 211 737 989 592 241 540 389 257 282

3)复杂它将每个分量转换为复数,然后对实部和虚部执行所需的算术运算:

3) complex This converts each component to a complex number and then performs the required arithmetic on the real and imaginary parts:

data_c <- as.complex(sub(":(\\d+)", "+\\1i", data))
60 * Re(data_c) + Im(data_c)
## [1] 211 737 989 592 241 540 389 257 282

3a)(3)的此变体也有效,并且避免使用正则表达式:

3a) This variation of (3) also works and avoids regular expressions:

data_c <- as.complex(paste0(chartr(":", "+", data), "i"))
60 * Re(data_c) + Im(data_c)
## [1] 211 737 989 592 241 540 389 257 282

4)eval 转换为ea将ch分量转换为算术表达式,该算术表达式的计算结果为分钟数,然后执行评估。当您可以避免使用 eval 时,实际上并不建议这样做,因此不太可取:

4) eval This converts each component into an arithmetic expression which evaluates to the number of minutes and then performs the evalution. Using eval is not really recommended when you can avoid it so this one is less desirable:

sapply(parse(text = sub("(\\d+):", "60*\\1+", data)), eval)
## [1] 211 737 989 592 241 540 389 257 282

5)POSIXlt 转换为 POSIXlt 类,然后使用 hour min 组件:

5) POSIXlt We can convert to "POSIXlt" class and then use the hour and min components:

with(unclass(as.POSIXlt(data, format = "%H:%M")), 60 * hour + min)
## [1] 211 737 989 592 241 540 389 257 282

6)chron 使用chron包,我们可以粘贴秒数,转换为 times 类,然后转换为分钟:

6) chron Using the chron package we can paste on the seconds, convert to "times" class and then convert to minutes:

library(chron)
24 * 60 * as.numeric(times(paste0(data, ":00")))
## [1] 211 737 989 592 241 540 389 257 282

7)lubridate 使用lubridate包,我们可以使用 hm ,然后取数字给出秒,最后除以60得到分钟:

7) lubridate Using the lubridate package we can convert it using hm and then to numeric giving seconds and finally dividing by 60 to give minutes:

as.numeric(hm(data)) / 60
## [1] 211 737 989 592 241 540 389 257 282

这篇关于将时间值转换为数字,同时保留时间特征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆