0

I have two data frames. First data frame contains time stamps has the start and end times (time durations between starting and ending times of these frames is equal),

whereas the second data frame has similar columns i.e. "a$start" and "a$end". However the time duration between the "v$start" and "v$end" is different in the second data frame. This second data frame has a third column called "v$label". I have to insert the "v$label" column into the first data frame as "a$label" if the following conditions are met.

(((v$START[i]>=a$START) & (v$END[i]<=a$END)) | ((v$START[i]<a$START) & (v$END[i]>a$END)) | ((v$START[i]<ann$START) & (v$END[i]>a$START)) | ((v$START[i]>ann$START) & (v$START[i]<a$END)))

I am trying the following code which basically subsets the dataframe "a" based on the above mentioned conditions and then stores the result in a list called "v_new" whose dimension is equal to the no. of rows of the dataframe "v". Afterwards, i can bind the list into a single dataframe.

v_new<-list()
for(i in 1:nrow(v)){

inter<-subset(a,(((v$START[i]>=a$START) & (v$END[i]<=a$END)) | ((v$START[i]<a$START) & (v$END[i]>a$END)) | ((v$START[i]<a$START) & (v$END[i]>a$START)) | ((v$START[i]>a$START) & (v$START[i]<a$END))))
      inter$v_label<-v$label[i]
      v_new[[i]]<-inter 
    }

Now the first problem is that for some rows of "v", even if the four conditions are met, the subset command returns the empty rows. I am giving the example where this actually happens

START = c("2016-07-09 10:29:53.000000") 
END = c("2016-07-09 10:34:40.400000") 
Label = c("S") 
v = data.frame(START, END, Label)
START=c("2016-07-09 10:30:00")
END=c("2016-07-09 10:35:00")
a=data.frame(START,END)  

If you run the code on these data frames, the result is an empty data frame. Is there anything wrong with the conditions that i mentioned in the beginning. The output should look something like this.

a$start                  a$end                  v$start           v$end                        v$label
2016-07-09 10:30:00 2016-07-09 10:35:00 2016-07-09 10:29:53.000000 2016-07-09 10:34:40.400000  C

Does anyone also know how to do it without loops. Any help will be deeply appreciated.

Gujj
  • 35
  • 8
  • 2
    Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap Jan 17 '18 at 13:44
  • 1
    In addition to the first comment, your conditions are unreadable. v$START[i]a$START means nothing. – MrSmithGoesToWashington Jan 17 '18 at 15:01
  • I have made necessary modifications as suggested by Jaap – Gujj Jan 18 '18 at 09:02

0 Answers0