4
votes

Thanks in advance!

Here is a sample (n = 10) of the dates I am working with. These are from an excel .csv. I need to convert them into a recognizable R format in order to perform basic arithmetic (days between etc). This is the output of 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

Running the above code should result in … my sample 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 

Following the example in the chron() help file I ran the following code

help.data <- as.character(help.data)
help = t(as.data.frame(strsplit(help.data,' ')))
row.names(help) = NULL

This all seems to work fine, but when I run the last bit of code (below) I continually get errors returned regarding format. As my formats are different than the examples I have changed the code, but still return errors.

library(chron)
help.done = chron(dates=help[,1],times=help[,2],
             format=c("m/d/y" , "h:m"))

The above format ("m/d/y" , "h:m") is how I want the data to look. Any help is greatly appreciated!!

3

3 Answers

5
votes

Or you could simply use as.POSIXct(help.data, format="%m/%d/%Y %H:%M", tz="GMT").

5
votes

Or:

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

The times have to be int he format h:m:s. This will work:

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