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

时间:2013-09-15 04:37:56

标签: r

提前致谢!

以下是我正在使用的日期的样本(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”)是我想要数据的样子。非常感谢任何帮助!!

3 个答案:

答案 0 :(得分:5)

或者:

library(lubridate)
mdy_hm(as.character(help.data))

答案 1 :(得分:4)

或者您只需使用as.POSIXct(help.data, format="%m/%d/%Y %H:%M", tz="GMT")

答案 2 :(得分:1)

时间必须采用h:m:s格式。这将有效:

chron(dates=dates(help[,1]),times=times(paste0(help[,2], ":00")),
             format=c("m/d/Y" , "h:m:s"))