0

I have a sequence from 1 to 2 which actually represents 1 to 2 minutes. I would like the increments to be in seconds. I don't think the 'chron' package will answer my problem here, because I want to make an array of numerical, decimal values not colon separated values (i.e.,1:01, 1:02).

     seq(from=1, to=2, by=0.006)

this is what I've been doing so far, at least it gives me a ratio. But I would actually just like seconds so 1.00 to 1.60 then 2 to 2.60 essentially.

is there another function that could help me out w/ this so I don't have to write an ifelse statement?

Doug
  • 597
  • 2
  • 7
  • 22
  • Why not just use `format` after getting the values with colons? – A5C1D2H2I1M1N2O1R2T1 Sep 11 '12 at 19:41
  • @mrdwab, I could but i wonder if there is a way to do this in 1 line of code. it's not like I care that much, I'm just curious if there was some one-step solution to my question – Doug Sep 11 '12 at 19:47

1 Answers1

2

You could use ?seq.POSIXt.

s <- seq(from=as.POSIXct("1", format="%M"), to=as.POSIXct("2", format="%M"), by="sec")
s
# [1] "2012-09-11 00:01:00 CEST" "2012-09-11 00:01:01 CEST" "2012-09-11 00:01:02 CEST" ...

To convert them into your target format look for ?strftime:

as.double(strftime(s, format="%M.%S"))
# [1] 1.00 1.01 1.02 ... 1.58 1.59 2.00
sgibb
  • 25,396
  • 3
  • 68
  • 74
  • I am actually trying something more like seq(from=as.POSIXct('1 - 1'...),to=as.POSIXct('1 + 2'...)...) even if I set those as objects outside of the function it won't work, I need to look into this but does anything come to your mind if I'm wanting to do some arithmetic – Doug Sep 11 '12 at 22:37
  • The first argument of `as.POSIXct` is of type `character` and you can't do arithmetic in `character`s. You have to do your arithmetic in using `double` values and converting them to `character` afterwards. e.g.: as.POSIXct(as.character(1+2), format="%M") – sgibb Sep 12 '12 at 16:14