2

I am not very familiar with using loops. I am trying to create a table like this: 00:00 00:01 00:02 ... 27:58 27:59 28:00

It is for data that runs into the next day, but is still considered part of the original day.

I tried this:

hh<-c(seq('00','28',1))  
mm<-c(seq('01','60',1))

for (i in (1:29)) {
  int<-paste(hh[i],":",mm)
}
View(int)

but it only does the pastes minutes 1 through 60 onto hour 28: only the 28th hour

I hope this is simpler than I am making it out to be, I don't know how to use the sapply or lapply to do this. Thanks for any help.

iRbaboon
  • 23
  • 4
  • I seem to be getting somewhere. I tried: ``` int<-c(sapply(hh, paste, mm)) View(int) ``` but I am missing the ":" in between. – iRbaboon Sep 17 '20 at 02:38

2 Answers2

1

You can do this with the help of outer :

times <- c(t(outer(sprintf('%02d', 0:27),sprintf('%02d', 0:59),paste,sep = ":")))
#add the last time manually
times <- c(times, '28:00')

head(times, 8)  
#[1] "00:00" "00:01" "00:02" "00:03" "00:04" "00:05" "00:06" "00:07"
tail(times, 8)  
#[1] "27:53" "27:54" "27:55" "27:56" "27:57" "27:58" "27:59" "28:00"

sprintf appends 0 to a single digit number and we paste every hour (0 to 27) to every minute (0 to 59).

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • would also `gsub(' ', '0', times)` or something since `For numbers, pad to the field width with leading zeros. For characters, this zero-pads on some platforms and is ignored on others.` – rawr Sep 17 '20 at 02:30
0

This worked for me as well:

hh<-c(seq('00','28',1))
mm<-c(seq('00','59',1))

library(stringr)
hh<-str_pad(hh, 2,pad="0")
mm<-str_pad(mm, 2,pad="0")

times<-c(sapply(hh, paste, sep=":",mm))
View(times)
iRbaboon
  • 23
  • 4