How to extarct hours and minutes from the factor time varaible

r

#1

Hello All ,

Greetings!!!

I am working on employee data in R-studio , check in and check out is the variables

Date i got separately i done format with that
I got the 2 varaible class as factor

ChekIn time data

09:43:00.0000000 – It comes with class of factor
09:23:00.0000000

I want to extract like this

09:43 and needed this in time format after extracting
09:23

I try to convert character and applied as.POSIXct to the variables but did not get any proper results since it is represented in factor

Can any one help on this

Thanks & Regards
Raghavendra


#2

Hi @raghava_r4u,

You can use the lubridate package to get your desired output.

library(lubridate)
ChekIn <- hms(ChekIN)
hm <- strftime(as.character(ChekIn), format="%H:%M")

In case you want to read the date and time variables in character format, you can provide the parameter stringsAsFactors = F with read.csv. It will prevent the conversion of date and time variables to factor.

Hope this helps.

Thanks,
Debarati.