I have a bunch of timestamps in R:

```
set.seed(42)  
t <- sample(1368104620:1399593658, 1000)
t <- as.POSIXlt(t, origin="1970-01-01")

```

I want to know how far each timestamp is from a fixed hour of the day (e.g. how far is the timestamp from 5am? [either 5am today, yesterday, or tomorrow]). The max distance should be 12.

```
h <- t$hour + t$min/60 + t$sec/3600
h_fixed <- 5

```

One method for doing this is to compare the current hour to the fixed\_hour, the fixed\_hour yesterday, and the fixed\_hour today.

```
d1 <- pmin(abs(h-h_fixed),abs(h-h_fixed+24),abs(h-h_fixed-24))
plot(h, d1)

```

Another, slightly fancier approach is as follows:

```
d2 <- pmin((h_fixed-h) %% 24, (h-h_fixed) %% 24)
plot(h, d2)
all.equal(d1, d2)

```

Is their a more elegant approach? I feel like I should be able to solve this without using pmin, but the answer eludes me.