Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert numeric to hours and minutes in R?

Tags:

r

I have a variable in the form of minutes spent in an activity. I would like to convert this number into hours and minutes by taking the digits into account but without producing seconds. Here's an example:

minutes<-c(81.4398, 50.9903,107.5511,433.3051,10.1234)
minutes

I would like the outcome in a clock format:

hrsmins<- c(1:21,0:51,1:48,7:13,0:10) 

Note that the outcome, does not show the clock time of the day but hours and minutes in the activity. I have a very large data set (millions of episodes). So, I would appreciate if someone can offer an efficient way of doing this conversion.

like image 798
Eva Avatar asked Jun 20 '16 12:06

Eva


1 Answers

We can try with chron

library(chron)
substr(times((minutes%/%60 +  minutes%%60 /60)/24), 1, 5)
#[1] "01:21" "00:50" "01:47" "07:13" "00:10"

Or it could be

sub(":\\d{2}", "", times((minutes%/%60 +  minutes%%60 /3600)/24))
#[1] "01:21" "00:51" "01:48" "07:13" "00:10"
like image 159
akrun Avatar answered Oct 16 '22 05:10

akrun