1
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..?

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Chanti
  • 525
  • 1
  • 5
  • 15
  • What is the question? – David Arenburg Feb 04 '15 at 10:46
  • how to subset by avoiding third_row(whose timestamp is already booked in second row) – Chanti Feb 04 '15 at 10:47
  • First, see how to create a reproducible example [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) (hint - DON'T provide images because no one is going type your data by hand). Second, take a look at `unique`. – David Arenburg Feb 04 '15 at 10:52
  • 1
    @DavidArenburg : thank you very much for the example link. It helped me writing a perfect question. once see the edited question – Chanti Feb 04 '15 at 11:03

2 Answers2

2

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
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
1

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,]
Rufo
  • 524
  • 1
  • 3
  • 18