Question Details

No question body available.

Tags

r string strftime

Answers (4)

February 2, 2026 Score: 2 Rep: 21,755 Quality: Low Completeness: 50%

It would help others to help you if you'd edit your question and include some example data, a small subset from ctout1 in SAS would probably do as it seems to include both timem and secondIndex. This would give us example input (timem) and expected output (secondIndex). A small reproducible sample from data.dt.cleaned in R would also help, just to know types of your columns. For this you can use output from dput(head(data.dt.cleaned)).

You may also want to evaluate parts of your expressions, I'd guess you'd be somewhat surprised by the result of strftime(00:00:05). And I'm also pretty sure length() does not what you may think it does. No need to go through 1-col matrix either.

February 2, 2026 Score: 1 Rep: 78,167 Quality: Medium Completeness: 80%

I believe you are misunderstanding what strftime(00:00:05) does.
Look at what its numeric argument is. It is a sequence created by operator :.

00:00:05
#> [1] 0 1 2 3 4 5

0:0:5 #> [1] 0 1 2 3 4 5

0:5 #> [1] 0 1 2 3 4 5

Created on 2026-02-02 with reprex v2.1.1

You have created a vector of length 6.
In this case you were lucky, the first and middle numbers are equal. If they were different:

00:02:05
#> Warning in 0:2:5: numerical expression has 3 elements: only the first used
#> [1] 0 1 2 3 4 5
0:2:5
#> Warning in 0:2:5: numerical expression has 3 elements: only the first used
#> [1] 0 1 2 3 4 5

Created on 2026-02-02 with reprex v2.1.1

The correct way of having strftime interpret 00:00:05 as a time would be the following.

d  [1] 1

Created on 2026-02-02 with reprex v2.1.1

Note that Sys.date() was chosen because it works, there is nothing special about it, any other existing year/month/day would have done the job.

February 2, 2026 Score: 0 Rep: 70,435 Quality: Low Completeness: 10%

I don’t know anything about SAS, but my understanding is that the SQL design requirements do not guarantee a specific row order beyond what is described in an ORDER_BY clause. So if that is ambiguous in your data (due to duplicate values driving the index), I’m not sure it will be replicable, at least not without a very extensive understanding of SAS’ SQL code (which could well vary between versions).

February 2, 2026 Score: 0 Rep: 70,435 Quality: Low Completeness: 50%

"I don't understand how to get seconds since midnight from a string time that is extracted from the time_m"

If you want to convert a time that is stored as text like "00:00:05" to seconds since midnight in R, you could use lubridate's hms() to parse the string and return that value (once coerced to numeric).

data.dt.cleaned