使用R将时间格式转换为数字

时间:2021-10-20 23:01:26

In most cases,we convert numeric time to posixct format using R. However, sometimes, we want to compare which time point is earlier, then we would prefer the numeric time format. Thus, it's quite practical question to convert time format to numeric. For example,I have the data format like "2001-03-13 10:31:00",

在大多数情况下,我们使用R将数字时间转换为posixct格式。但是,有时,我们想要比较哪个时间点更早,然后我们更喜欢数字时间格式。因此,将时间格式转换为数字是非常实际的问题。例如,我的数据格式如“2001-03-13 10:31:00”,

  begin <- "2001-03-13 10:31:00"

Using R, I want to figure out how to covert it into a numeric, e.g. the Julian time, something like the passing seconds between 1970-01-01 00:00:00 and 2001-03-13 10:31:00.

使用R,我想弄清楚如何将其转换为数字,例如朱利安时间,如1970-01-01 00:00:00和2001-03-13 10:31:00之间的秒数。

Do you have any suggestions?

你有什么建议吗?


The Julian calendar began in 45 BC (709 AUC) as a reform of the Roman calendar by Julius Caesar. It was chosen after consultation with the astronomer Sosigenes of Alexandria and was probably designed to approximate the tropical year (known at least since Hipparchus). see http://en.wikipedia.org/wiki/Julian_calendar

朱利安历法始于公元前45年(70年代AUC),作为朱利叶斯·凯撒对罗马历法的改革。它是在与亚历山大的天文学家Sosigenes协商后选择的,可能是为了接近热带年份(至少从Hipparchus开始就已知)。见http://en.wikipedia.org/wiki/Julian_calendar

3 个解决方案

#1


8  

If you jsut want to remove ":" , " ", and "-" from a character vector then this will suffice:

如果你想从字符向量中删除“:”,“”和“ - ”,那么这就足够了:

end <- gsub("[: -]", "" , begin, perl=TRUE)
#> end
#[1] "20010313103100"

You should read the section about 1/4 of the way down in ?regex about character classes. Since the "-" is special in that context as a range operator, it needs to be placed first or last.

你应该阅读关于字符类的1/4向下的部分。由于“ - ”在该上下文中作为范围运算符是特殊的,因此需要将其放在第一个或最后一个。

After your edit then the answer is clearly what @joran wrote, except that you would need first to convert to a DateTime class:

在编辑之后,答案显然是@joran写的,除了你需要先转换为DateTime类:

 as.numeric(as.POSIXct(begin))
#[1] 984497460

The other point to make is that comparison operators do work for Date and DateTime classed variables so the conversion may not be necessary at all. This compares 'begin' to a time one second later and correctly reports that begin is earlier:

另一个要点是比较运算符确实适用于Date和DateTime分类变量,因此根本不需要转换。这将“开始”与一秒钟后的时间进行比较,并正确报告开始时间较早:

as.POSIXct(begin) < as.POSIXct(begin) +1
 #[1] TRUE

#2


4  

Based on the revised question this should do what you want:

根据修订后的问题,这应该做你想要的:

begin <- "2001-03-13 10:31:00"
as.numeric(as.POSIXct(begin))

The result is a unix timestamp, the number of seconds since epoch, assuming the timestamp is in the local time zone.

结果是一个unix时间戳,即纪元以来的秒数,假设时间戳在本地时区。

#3


0  

The example from ?as.POSIX help gives

来自?as.POSIX帮助的例子给出了

as.POSIXct(strptime(begin, "%Y-%m-%d %H:%M:%S"))

so for you it would be

所以对你来说就是这样

as.numeric(as.POSIXct(strptime(begin, "%Y-%m-%d %H:%M:%S")))

#1


8  

If you jsut want to remove ":" , " ", and "-" from a character vector then this will suffice:

如果你想从字符向量中删除“:”,“”和“ - ”,那么这就足够了:

end <- gsub("[: -]", "" , begin, perl=TRUE)
#> end
#[1] "20010313103100"

You should read the section about 1/4 of the way down in ?regex about character classes. Since the "-" is special in that context as a range operator, it needs to be placed first or last.

你应该阅读关于字符类的1/4向下的部分。由于“ - ”在该上下文中作为范围运算符是特殊的,因此需要将其放在第一个或最后一个。

After your edit then the answer is clearly what @joran wrote, except that you would need first to convert to a DateTime class:

在编辑之后,答案显然是@joran写的,除了你需要先转换为DateTime类:

 as.numeric(as.POSIXct(begin))
#[1] 984497460

The other point to make is that comparison operators do work for Date and DateTime classed variables so the conversion may not be necessary at all. This compares 'begin' to a time one second later and correctly reports that begin is earlier:

另一个要点是比较运算符确实适用于Date和DateTime分类变量,因此根本不需要转换。这将“开始”与一秒钟后的时间进行比较,并正确报告开始时间较早:

as.POSIXct(begin) < as.POSIXct(begin) +1
 #[1] TRUE

#2


4  

Based on the revised question this should do what you want:

根据修订后的问题,这应该做你想要的:

begin <- "2001-03-13 10:31:00"
as.numeric(as.POSIXct(begin))

The result is a unix timestamp, the number of seconds since epoch, assuming the timestamp is in the local time zone.

结果是一个unix时间戳,即纪元以来的秒数,假设时间戳在本地时区。

#3


0  

The example from ?as.POSIX help gives

来自?as.POSIX帮助的例子给出了

as.POSIXct(strptime(begin, "%Y-%m-%d %H:%M:%S"))

so for you it would be

所以对你来说就是这样

as.numeric(as.POSIXct(strptime(begin, "%Y-%m-%d %H:%M:%S")))