How to extarct hours and minutes from the factor time varaible



Hello All ,


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

I want to extract like this

09:43 and needed this in time format after extracting

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


Hi @raghava_r4u,

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

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.