Convert an integer column to time HH:MM
Solution 1
Here's one suggestion
temp <- c(0 , 5 , 10, 15 ,20 , 25 ,30 ,35, 40, 45 , 50 ,55 ,100 ,105, 110) # Your data
temp2 <- mapply(function(x, y) paste0(rep(x, y), collapse = ""), 0, 4 - nchar(temp))
temp <- paste0(temp2, temp)
temp
# [1] "0000" "0005" "0010" "0015" "0020" "0025" "0030" "0035" "0040" "0045" "0050" "0055" "0100" "0105" "0110"
Then you can do
format(strptime(temp, format="%H%M"), format = "%H:%M")
#[1] "00:00" "00:05" "00:10" "00:15" "00:20" "00:25" "00:30" "00:35" "00:40" "00:45" "00:50" "00:55" "01:00" "01:05" "01:10"
Solution 2
Slightly modified version of @David Arenburg's code that uses sprintf
(see this blog post for differences in paste
and sprint
: http://trinkerrstuff.wordpress.com/2013/09/15/paste-paste0-and-sprintf-2/)"
temp <- c(0 , 5 , 10, 15 ,20 , 25 ,30 ,35, 40, 45 , 50 ,55 ,100 ,105, 110) # Your data
temp <- sprintf("%04d", temp)
## [1] "0000" "0005" "0010" "0015" "0020" "0025" "0030" "0035"
## [9] "0040" "0045" "0050" "0055" "0100" "0105" "0110"
format(strptime(temp, format="%H%M"), format = "%H:%M")
## [1] "00:00" "00:05" "00:10" "00:15" "00:20" "00:25" "00:30" "00:35"
## [9] "00:40" "00:45" "00:50" "00:55" "01:00" "01:05" "01:10"
Admin
Updated on June 20, 2022Comments
-
Admin almost 2 years
I'm trying to learn R, and I'm stuck with a problem regarding the conversion of one column in a data set from integer values to time.
The aforementioned column breaks the days in 5 minute portions. Using the following format: 5 would be 00:05, 105 would be 01:05 and 1105 would be 11:05.
if I use:
strptime(activity[,"interval"],format="%H%M")
The resulting object returns "NA" for all the values that are below 1000.
Any ideas on how to make that same process using apply family would be greatly appreciated
I know this is a pretty basic question but I am not able to figure it out myself.
Thank you very much
Edit: As requested the activity[n,"interval"] column (This column has 17568 rows, comprising numbers from 5 to 2355 for several days) and the 15 first elements look like this:
activity[1:15,"interval"] [1] 0 5 10 15 20 25 30 35 40 45 50 55 100 105 110
And should look like this
activity[1:15,"interval"] [1] 0000 0005 0010 0015 0020 0025 0030 0035 0040 0045 [11] 0050 0055 0100 0105 0110
-
Admin over 9 yearsYes just my fault judging the code, my apologies. It works perfectly. Thank you very much!
-
Admin over 9 yearsTHanks for the link to the article I found it very useful
-
Admin over 9 yearsI was advancing on this piece of code and I noticed that the 2nd chunk of code in your answer does return a character vector, is there any way that this character becomes a date vector? I need something that is not discrete in order to plot it as a line plot.
-
David Arenburg over 9 yearsI don't think there is a time class in R without being associated with a date. Thus, you probably should use
strptime(temp, format="%H%M")
for your line plot and then format thex
scale withformat(strptime(temp, format="%H%M"), format = "%H:%M")
. This can be easily done withggplot2
pckae for example -
Admin over 9 yearsWhat I have done at the end is using the original values for the plot and then renaming manually the axis. I did everithing with base plotting because I havent yet tried ggplot2 or lattice. Thank you anyways for all the useful information David! You have been very helpful.