Integer column conversion during HH: MM

I am trying to learn R, and I had a problem converting one column to a dataset from integer values ​​in time.

The above column breaks days in 5-minute servings. Using the following format: 5 will be 00:05, 105 will be 01:05, and 1105 will be 11:05.

if i use:

strptime(activity[,"interval"],format="%H%M")

The resulting object returns "NA" for all values ​​that are below 1000.

Any ideas on how to do the same process using the application family would be greatly appreciated.

I know this is a pretty simple question, but I can't figure it out myself.

Many thanks

Edit: upon request, the activity column [n, "interval"] (this column contains 17568 rows containing numbers from 5 to 2355 for several days), and the first 15 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 it 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
+4
source share
2 answers

Here is 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"
+5
source

A slightly modified version of @David Arenburg's code that uses sprintf(see this blog post for differences in pasteand 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"
+4
source

All Articles