Convert numeric value to R
I was given a file with time in minutes.seconds in the following format:
TimeInput <- c(13.66, 14.08, 12.86)
How can I convert a numeric value like 13.66 to minutes: seconds format. Specifically 14:06 or 14 minutes and 6 seconds?
My expected output:
TimeInput <- c(14:06, 14:08, 13:26)
+3
source to share
3 answers
We can try without using an external package
v1 <- (TimeInput-floor(TimeInput)) - 0.60
v2 <- ifelse(v1 > 0, floor(TimeInput) + 1 + v1, TimeInput)
d1 <- data.frame(MinSec = sub(".", ":", v2, fixed = TRUE),
TimeRound = floor(v2), stringsAsFactors=FALSE)
d1
# MinSec TimeRound
#1 14:06 14
#2 14:08 14
#3 13:26 13
+1
source to share
You can achieve this by using POSIX.ct
and the format
TimeInput <- c(13.66, 14.08, 12.86,0)
pct <- as.POSIXct(TimeInput*60,origin = "1960-01-01 00:00:00","GMT")
format(pct,format="%M:%S")
[1] "13:40" "14:05" "12:52" "00:00"
The solution is a little annoying due to the timezone / need to set an arbitrary origin. On the positive side, this applies to all kinds of problems (?) Like overflows. If it's intended
If, however, you want to have minutes above 60
+1
source to share