gpt4 book ai didi

r - 从日期和小时格式更改为数字格式

转载 作者:行者123 更新时间:2023-12-04 00:11:56 25 4
gpt4 key购买 nike

我在 R 中工作,我需要更改格式列

9/27/2011  3:33:00 PM 

到值格式。在 Excel 中,我可以使用函数 value()但我不知道如何在 R 中做到这一点。

我的数据如下所示:
9/27/2011 15:33 a   1   5   9
9/27/2011 15:33 v 2 6 2
9/27/2011 15:34 c 3 7 1

最佳答案

要将字符串转换为 R 日期格式,请使用 as.POSIXct - 然后您可以使用 as.numeric 将其强制为数值:

> x <- as.POSIXct("9/27/2011  3:33:00 PM", format="%m/%d/%Y  %H:%M:%S %p")
> x
[1] "2011-09-27 03:33:00 BST"
> as.numeric(x)
[1] 1317090780

您获得的值表示自任意日期(通常为 1/1/1970)以来的秒数。请注意,这与 Excel 不同,其中日期存储为自任意日期以来的天数(1/1/1900,如果我没记错的话 - 我尽量不再使用 Excel。)

如需更多信息,请参阅 ?DateTimeClasses

关于r - 从日期和小时格式更改为数字格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8215404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com