2

Here is a subset of my data:

Fr         Sig  Code NumDet    Date.Time          Aerial
62  150102 102   15    195 2012-09-14 18:28:00      1
63  150102 102   15    189 2012-09-14 18:32:00      1
64  150102 106   15    213 2012-09-14 18:36:00      1
65  150102 102   15    152 2012-09-14 18:40:00      1
66  150102 105   15    190 2012-09-14 18:46:00      1
67  150102  97   15      4 2012-09-14 18:51:00      2

I am trying to calculate time between first detection on Aerial 1 and first detection on Aerial 2. Hence in this data set it would be 23mins

I have tried variations of difftime but can't seem to select specific times based on the Aerial number.

I have tried:

a <- difftime(table$Date.Time[2:length(table$Aerial == "1")], 
              table$Date.Time[2:length(table$Aerial == "2")])

but it's not even close.

HeatfanJohn
  • 7,143
  • 2
  • 35
  • 41
Salmo salar
  • 517
  • 1
  • 5
  • 17

1 Answers1

1

This command using difftime

difftime(table$Date.Time[table$Aerial == "2"][1],
         table$Date.Time[table$Aerial == "1"][1])

will return

Time difference of 23 mins
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
  • Thats great, thanks! didnt realise i was so close, i'm new to R... am i right in thinking the [1] selects the first time the aerial number occurs? Thanks again! – Salmo salar Oct 31 '12 at 21:33
  • What would you replace the '[1]' with the select the last element rather than the first? have tried 'length(x)' but wont work – Salmo salar Oct 31 '12 at 23:25
  • @user1751379 You can select the last element of object `x` with the command `tail(x, 1)`. Using your example: `difftime(tail(table$Date.Time[table$Aerial == "2"], 1), tail(table$Date.Time[table$Aerial == "1"], 1))` – Sven Hohenstein Nov 01 '12 at 07:04