Get Seconds from timestamp in R

In Order to get seconds from timestamp in R we will be using as.numeric () and Format() function. Let’s see how to get seconds 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

Get seconds from time stamp in R 1

 

Get Seconds from timestamp in R

Seconds is extracted from timestamp (column: Logout_time)  using as.numeric () and Format() functions as shown below.

df1$Seconds_component <- as.numeric(format(df1$Logout_time,'%S'))
df1

So the resultant data frame has a column Seconds_component with Seconds extracted from timestamp.

Get seconds from time stamp in R 2

 

                                                                                                               

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.