Calculating time lapse specific to another column variable.

Im new with R and am struggling a bit, but refuse to use minitab or excel as I want to learn R. I have a data set in excel that has dates and time in one column and process steps in another. I want to calculate the total time the process was in a specific step number, but can't figure it out. In the example below, how would I calculate the total time the process was in step 500?

Thanks in advance

8/13/19 4:39:25 PM 0 0
8/13/19 4:49:25 PM 0 500
8/13/19 4:59:25 PM 0 500
8/13/19 5:09:25 PM 0 500
8/13/19 5:19:25 PM 0 500
8/13/19 5:29:25 PM 0 500
8/13/19 5:39:25 PM 0 500
8/13/19 5:49:25 PM 0 500
8/13/19 5:59:25 PM 0 500
8/13/19 6:09:25 PM 0 500
8/13/19 6:19:25 PM 0 500
8/13/19 6:29:25 PM 0 500
8/13/19 6:39:25 PM 0 500
8/13/19 6:49:25 PM 0 500
8/13/19 6:59:25 PM 0 500
8/13/19 7:09:25 PM 0 500
8/13/19 7:19:25 PM 0 500
8/13/19 7:29:25 PM 0 500
8/13/19 7:39:25 PM 0 500
8/13/19 7:49:25 PM 0 500
8/13/19 7:59:25 PM 0 500
8/13/19 8:09:25 PM 0 500
8/13/19 8:19:25 PM 0 500
8/13/19 8:29:25 PM 0 0
8/13/19 8:39:25 PM 0 0
8/13/19 8:49:25 PM 0 0
8/13/19 8:59:25 PM 0 0
8/13/19 9:09:25 PM 0 0

1 Like

Hi, and welcome!

The first step will be importing your data into an R data frame. Then confirm that your PM column is a date class

library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#> 
#>     date

leave <- ymd_hms("2011-08-10 14:00:00", tz = "Pacific/Auckland")
 
class(leave)
#> [1] "POSIXct" "POSIXt"

Created on 2019-12-19 by the reprex package (v0.3.0)

You'll want to install lubridate. The package has since been updated, but the introduction will give you a good orientation.

Once you have PM right, you'll be working with duration objects.

When you get stuck, come back with a new post including a reproducible example, called a reprex for more help.

Good decision to learn R! (I'm biased, as a long-term user.) I hope you find it a useful and fun as I have.

Thanks. I already had libridate installed. Not sure if this correct.

leave <- ymd_hms("2018-12-19 09:49:25", tz = "America/Chicago")
#> Error in ymd_hms("2018-12-19 09:49:25", tz = "America/Chicago"): could not find function "ymd_hms"

class(leave)
#> Error in eval(expr, envir, enclos): object 'leave' not found

Created on 2019-12-19 by the reprex package (v0.3.0)

Sure that you had lubridate loaded? In R there are some packages that are always available by default, but most you have to get in your workspace explicitly

library(lubridate)
leave <- ymd_hms("2018-12-19 09:49:25", tz = "America/Chicago")

got it to work this time. Now I just need to get time durations when process step 50 is used.

library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following object is masked from 'package:base':
#>
#> date
leave <- ymd_hms("2018-12-19 09:49:25", tz = "America/Chicago")

1 Like

Great! Come back with a new post with a reproducible example, called a reprex if you get stuck on implementing your calculation.

I still cant figure it out. I guess I need to start out with much much simpler task.

How far did you get? Where are you stuck?

I gave up for now. I went back to the basics just trying to understand the basics. I can't figure out how to calculate duration between two date/time entries.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.