gpt4 book ai didi

r - 使用 chron() 将日期/时间戳转换为正确的 R 格式

转载 作者:行者123 更新时间:2023-12-04 22:32:09 25 4
gpt4 key购买 nike

提前致谢!

这是我正在使用的日期的示例 (n = 10)。这些来自 excel .csv。我需要将它们转换为可识别的 R 格式,以便执行基本的算术运算(天数等)。这是 dput() 的输出。

help.data <- structure(c(55L, 49L, 50L, 52L, 51L, 114L, 53L, 59L, 63L, 48L
), .Label = c("1/15/2012 8:00", "1/16/2012 6:00", "1/18/2012 10:00",
"1/5/2012 4:00", "1/5/2012 8:00", "1/7/2012 2:00", "1/8/2012 22:01",
"1/9/2012 16:01", "10/1/2011 4:00", "10/12/2011 8:00", "10/18/2011 0:00",
"10/19/2011 6:01", "10/2/2011 6:00", "10/21/2011 4:00", "10/21/2011 6:01",
"10/23/2011 6:01", "10/25/2011 14:00", "10/29/2011 4:01", "10/3/2011 4:00",
"10/30/2011 6:01", "10/31/2011 6:00", "10/5/2011 2:00", "10/8/2011 2:00",
"11/1/2011 0:00", "11/16/2011 10:00", "11/19/2011 10:00", "11/19/2011 6:00",
"11/25/2011 10:01", "11/27/2011 10:00", "11/9/2011 14:01", "12/1/2011 2:01",
"12/1/2011 8:00", "12/10/2011 4:03", "12/13/2011 2:01", "12/15/2011 4:00",
"12/16/2011 4:00", "12/18/2011 14:01", "12/22/2011 6:00", "12/25/2011 0:01",
"12/25/2011 6:00", "12/27/2011 22:00", "12/5/2011 8:01", "12/9/2011 8:01",
"2/3/2012 2:00", "2/3/2012 8:01", "2/5/2012 8:00", "2/9/2012 6:01",
"3/11/2011 8:00", "3/13/2011 8:00", "3/14/2011 0:02", "3/14/2011 10:00",
"3/14/2011 8:01", "3/15/2011 20:02", "3/17/2011 8:00", "3/18/2011 4:01",
"3/19/2011 10:01", "3/19/2011 12:00", "3/21/2011 4:00", "3/24/2011 10:00",
"3/26/2011 4:01", "3/27/2011 6:00", "3/29/2011 12:00", "3/4/2011 6:00",
"4/1/2011 0:00", "4/12/2011 8:00", "4/14/2011 10:00", "4/17/2011 10:00",
"4/21/2011 18:01", "4/29/2011 6:00", "4/3/2011 8:00", "4/4/2011 6:02",
"4/6/2011 8:00", "5/10/2011 12:00", "5/13/2011 6:00", "5/16/2011 8:00",
"5/19/2011 14:00", "5/2/2011 14:00", "5/20/2011 6:00", "5/23/2011 4:00",
"5/24/2011 16:02", "5/24/2011 4:00", "5/26/2011 4:01", "5/27/2011 4:01",
"5/28/2011 14:00", "5/31/2011 16:01", "5/31/2011 4:01", "5/9/2011 4:01",
"5/9/2011 8:00", "6/10/2011 12:00", "6/12/2011 12:00", "6/13/2011 6:02",
"6/15/2011 16:01", "6/15/2011 4:02", "6/17/2011 2:01", "6/19/2011 6:00",
"6/21/2011 8:01", "6/23/2011 20:01", "6/23/2011 22:01", "6/23/2011 4:02",
"6/26/2011 10:01", "6/26/2011 4:01", "6/27/2011 2:01", "6/28/2011 2:02",
"6/29/2011 10:00", "6/29/2011 6:01", "6/3/2011 10:01", "6/4/2011 6:00",
"6/7/2011 20:02", "6/7/2011 22:01", "7/1/2011 4:01", "7/10/2011 20:01",
"7/10/2011 6:01", "7/12/2011 6:01", "7/13/2011 8:01", "7/16/2011 4:00",
"7/16/2011 8:00", "7/17/2011 8:01", "7/18/2011 14:00", "7/2/2011 10:01",
"7/2/2011 2:01", "7/21/2011 4:01", "7/23/2011 6:01", "7/24/2011 16:02",
"7/27/2011 4:00", "7/28/2011 4:01", "7/31/2011 8:00", "7/4/2011 4:01",
"7/7/2011 0:00", "7/8/2011 4:00", "7/9/2011 12:01", "8/10/2011 6:01",
"8/11/2011 2:00", "8/12/2011 2:02", "8/15/2011 6:01", "8/18/2011 4:00",
"8/2/2011 8:00", "8/20/2011 4:00", "8/20/2011 8:00", "8/24/2011 4:01",
"8/25/2011 18:01", "8/29/2011 6:01", "8/3/2011 6:02", "8/30/2011 4:01",
"8/4/2011 4:00", "8/5/2011 6:00", "8/6/2011 4:00", "8/8/2011 6:01",
"9/1/2011 4:01", "9/10/2011 2:00", "9/14/2011 8:02", "9/19/2011 6:02",
"9/2/2011 10:01", "9/20/2011 4:01", "9/20/2011 8:00", "9/22/2011 10:01",
"9/22/2011 16:00", "9/27/2011 4:00", "9/28/2011 2:00", "9/29/2011 4:00",
"9/3/2011 10:00", "9/3/2011 8:01", "9/30/2011 6:00", "9/4/2011 10:01",
"9/7/2011 8:01", "9/9/2011 8:00"), class = "factor")
help.data

运行上面的代码应该会得到……我的示例数据。
 [1] 3/18/2011 4:01  3/13/2011 8:00  3/14/2011 0:02  3/14/2011 8:01 
[5] 3/14/2011 10:00 7/13/2011 8:01 3/15/2011 20:02 3/24/2011 10:00
[9] 3/4/2011 6:00 3/11/2011 8:00

按照 chron() 帮助文件中的示例,我运行了以下代码
help.data <- as.character(help.data)
help = t(as.data.frame(strsplit(help.data,' ')))
row.names(help) = NULL

这一切似乎都很好,但是当我运行最后一点代码(如下)时,我不断收到有关格式的错误返回。由于我的格式与示例不同,我更改了代码,但仍然返回错误。
library(chron)
help.done = chron(dates=help[,1],times=help[,2],
format=c("m/d/y" , "h:m"))

上面的格式 ("m/d/y", "h:m") 是我希望数据的外观。任何帮助是极大的赞赏!!

最佳答案

或者您可以简单地使用 as.POSIXct(help.data, format="%m/%d/%Y %H:%M", tz="GMT")

关于r - 使用 chron() 将日期/时间戳转换为正确的 R 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18808958/

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