-1

I have a dataframe like this:

     auftreten_Fehler Diff_Fehler   auftreten_Warnung Diff_Warnung
1 2016-08-10 17:35:25    207.0000 2016-08-10 17:08:56     207.0000
2 2016-08-29 19:16:10    226.0000 2016-08-29 19:15:48     226.0000
3 2016-08-30 19:38:28    234.0000 2016-08-30 19:38:24     234.0000
4 2016-10-22 00:30:20    204.3333 2016-10-20 20:05:11     224.0000
5 2016-10-26 19:19:42    234.0000 2016-10-26 19:16:27     234.0000
6 2016-10-27 15:31:44    235.0000 2016-10-27 05:21:46     235.0000
7 2016-10-28 10:39:28    204.3333 2016-10-28 10:39:24     204.3333
8 2016-11-07 19:12:26    207.0000 2016-11-07 19:12:18     207.0000
9 2016-11-16 11:14:51    204.3333 2016-11-11 09:18:46     213.0000

I would like to show only the rows with this condition:

k<-(FehlerWarnung[i,]$auftreten_Fehler - FehlerWarnung[i,]$auftreten_Warnung)
units(k)<-"mins" 
if(k<2)  #Show only rows if the difference is less than two minuts

How can I do this job with àpply` or something else in one row of code?

Sotos
  • 51,121
  • 6
  • 32
  • 66
Kaja
  • 2,962
  • 18
  • 63
  • 99

2 Answers2

1

Use difftime to do the time difference computation in one step, then index your rows directly like this.

FehlerWarnung[difftime(FehlerWarnung$auftreten_Fehler, FehlerWarnung$auftreten_Warnung, units='mins') < 2,]
mpjdem
  • 1,504
  • 9
  • 14
1

Wasn't comfortable with the german words so created a dummy dta from your. Sorry

difftime() does the job. You can also specify the Unite of difference you want(default is secs)

df
                    a                   b
1 2016-08-10 17:35:25 2016-08-10 17:08:56
2 2016-08-29 19:16:10 2016-08-29 19:15:48
3 2016-08-30 19:38:28 2016-08-30 19:38:24  

#difftime(df$a, df$b)
#Time differences in secs
#[1] 1589   22    4


df[difftime(df$a, df$b)>120,]
joel.wilson
  • 8,243
  • 5
  • 28
  • 48