1

In my very large dataframe, I have time in the following minutes:seconds format:

Time          Period          
6:21          1
8:52          2
15:00         2
...

How can I make it so that the new time column reads only the total number of seconds in the time column?

Time          Period      Seconds    
6:21          1             381
8:52          2             532
15:00         2             900

Further, how could I make it so that the Seconds variable is multiplied by the Period Variable (to display the total number of seconds passed)?

Time          Period      Seconds     TimePassed
6:21          1             381         381
8:52          2             532         1064
15:00         2             900         1800
887
  • 599
  • 3
  • 15

1 Answers1

0

Here is an option with period_to_seconds after converting the Time column to Period class with ms. Then, we multiply the 'Seconds' and 'Period' to create the 'TimePassed'

library(lubridate)
df1$Seconds <- period_to_seconds(ms(df1$Time))
df1$TimePassed <- df1$Seconds * df1$Period

-output

df1
#   Time Period Seconds TimePassed
#1  6:21      1     381        381
#2  8:52      2     532       1064
#3 15:00      2     900       1800

data

df1 <- structure(list(Time = c("6:21", "8:52", "15:00"), Period = c(1L, 
2L, 2L)), class = "data.frame", row.names = c(NA, -3L))
akrun
  • 874,273
  • 37
  • 540
  • 662