Chanti
Chanti

Reputation: 575

R programming - subsetting time specific data

C
                   State         State.Start           State.End State.Duration
1  UDT/Maintenance delay 01.02.2015 08:00:00 01.02.2015 08:10:00            600
2  UDT/Maintenance delay 01.02.2015 08:10:00 01.02.2015 08:40:00           1800
9            No material 01.02.2015 08:20:00 01.02.2015 08:40:00           1200
29 UDT/Maintenance delay 01.02.2015 14:00:00 01.02.2015 14:45:00           2700
35           No material 01.02.2015 16:00:00 01.02.2015 17:40:00           6000
45           No material 01.02.2015 17:30:00 01.02.2015 17:40:00            600
72           No material 01.02.2015 21:01:00 01.02.2015 23:30:00           8940
81           No material 01.02.2015 22:00:00 02.02.2015 01:38:00          13080
88       ENG/Engineering 01.02.2015 23:30:00 02.02.2015 01:11:00           6060
93 SDT/Maintenance delay 02.02.2015 02:30:00 02.02.2015 04:25:00           6900
95       ENG/Engineering 02.02.2015 04:25:00 02.02.2015 04:25:00              0

The above data is provided by a machine
I want to perform the subsetting operation

Delay_Time <- subset(C, State == "No material")  

and my output is

Delay_Time
         State         State.Start           State.End State.Duration
9  No material 01.02.2015 08:20:00 01.02.2015 08:40:00           1200
35 No material 01.02.2015 16:00:00 01.02.2015 17:40:00           6000
45 No material 01.02.2015 17:30:00 01.02.2015 17:40:00            600
72 No material 01.02.2015 21:01:00 01.02.2015 23:30:00           8940
81 No material 01.02.2015 22:00:00 02.02.2015 01:38:00          13080

if you see in the above Delay_time subset
time period of second row is 16:00:00 to 17:40:00
and time period of third row is 17:30:00 to 17:40:00
the third row time is already a part of second row(booked)
In my subset I dont want to have these third row like cases
is it possible to subset by avoiding the cases like third row..?

Upvotes: 1

Views: 64

Answers (2)

Rufo
Rufo

Reputation: 534

Sorry, I'm not used to use subset() function. Here you have a solution using [].

s_e_aux    <- C$State.End[C$State!="No material"]
Delay_Time <- C[C$State=="No material" & !C$State.End%in%s.e.aux,]

Upvotes: 1

David Arenburg
David Arenburg

Reputation: 92292

Here's a simple/efficient solution using the data.table package

library(data.table)
unique(setDT(C)[State == "No material"], by = "State.End")
#          State         State.Start           State.End State.Duration
# 1: No material 01.02.2015 08:20:00 01.02.2015 08:40:00           1200
# 2: No material 01.02.2015 16:00:00 01.02.2015 17:40:00           6000
# 3: No material 01.02.2015 21:01:00 01.02.2015 23:30:00           8940
# 4: No material 01.02.2015 22:00:00 02.02.2015 01:38:00          13080

Or a similar solution using dplyr

library(dplyr)
C %>%
  filter(State == "No material") %>%
  distinct(State.End)
#         State         State.Start           State.End State.Duration
# 1 No material 01.02.2015 08:20:00 01.02.2015 08:40:00           1200
# 2 No material 01.02.2015 16:00:00 01.02.2015 17:40:00           6000
# 3 No material 01.02.2015 21:01:00 01.02.2015 23:30:00           8940
# 4 No material 01.02.2015 22:00:00 02.02.2015 01:38:00          13080

Upvotes: 2

Related Questions