How to extract the hour component in R

I have following code and as you can see system calculating the difference correctly, but I would like consider only first 5 character i.e. 3.449 etc and I like to discard the ‘hours’ part.
Once I try to convert the same as character, then it not working.
Any help will be appreciated.


df %>%
  mutate(difference = difftime(DateTime_Stat, DateTime_End, units = 'hours'))

#       DateTime_Start        DateTime_End     difference
#1 2021-02-02 16:42:11 2021-02-02 16:43:15 0.000000 hours
#2 2021-02-02 20:10:14 2021-02-02 20:11:55 3.449754 hours

I have tried to convert the value to character type so I can use substr() to extract the values but system is failing to convert it to as character.

>Solution :

You can convert to numeric and round:

round(as.numeric(df$difference), 3)

Leave a Reply