In Order to extract time from timestamp in R we will be using Format() function. Let’s see how to get time from timestamp in R with an example.
Let’s first create the dataframe.
### Create Data Frame df1 = data.frame ( Name =c('Annie','Catherine','Teresa','Peterson','Richard','joe'), Login_time = as.POSIXct(c('2018-06-03 19:40:00','2018-06-03 11:30:30','2018-06-03 2:04:13','2018-06-03 16:44:43','2018-06-03 7:14:43','2018-06-03 17:12:41')), Logout_time = as.POSIXct(c('2018-06-03 21:40:34','2018-06-03 12:40:10','2018-06-03 3:44:23','2018-06-03 17:34:13','2018-06-03 9:34:23','2018-06-03 18:32:41'))) df1
dataframe df1 will be
Extract time from timestamp in R:
Time is extracted from timestamp (column: Logout_time) using Format() function as shown below.
df1$time_component <- format(df1$Logout_time,'%H:%M:%S') df1
So the resultant data frame has a column time_component with time extracted from timestamp