Назначение интервала DateTime событию DateTime - PullRequest
0 голосов
/ 25 октября 2019

У меня есть таблица> 1M отдельных интервалов обнаружения DateTime (то есть человек постоянно обнаруживался в течение этого времени) и таблица DateTime Intervals, которые представляют собой, когда ворота были закрыты. Отдельные обнаружения классифицируются как «IN» или «OUT»

Используя% в%, я смог определить, попадает ли обнаружение в ЛЮБОЙ из интервалов.

Однако яЯ хотел бы сделать что-то немного другое. Для каждого интервала закрытых ворот я хотел бы знать, сколько людей было обнаружено снаружи и сколько внутри. Я считаю, что самый простой способ сделать это - назначить каждому событию обнаружения период шлюза, но я не могу понять, как эта функция была бы написана без чрезвычайно грязных вложенных операторов ifelse.

ожидаемый вывод (не реальные данные):

  Tag     site  species      StartDateTime_UTC   EndDateTime_UTC     interval Location
   <fct>   <fct> <chr>        <dttm>              <dttm>                 <dbl> <chr>   
 1 5004.24 IC1   Striped Bass 2014-09-29 22:40:40 2014-09-29 22:46:35        1 IN      
 2 5004.24 IC1   Striped Bass 2014-09-29 22:49:15 2014-09-29 22:50:05        1 IN      
 3 5004.24 RGD1  Striped Bass 2014-10-01 23:01:12 2014-10-01 23:11:23        2 IN      
 4 5004.24 RGD1  Striped Bass 2014-10-01 23:16:18 2014-10-02 00:13:17        2 IN      
 5 5004.24 RGD1  Striped Bass 2014-10-02 00:15:47 2014-10-02 00:30:08        2 IN      
 6 5004.24 RGD1  Striped Bass 2014-10-02 00:33:12 2014-10-02 01:10:21        2 IN      
 7 5004.24 RGD1  Striped Bass 2014-10-02 01:13:01 2014-10-02 01:20:12        2 IN      
 8 5004.24 RGD1  Striped Bass 2014-10-02 04:14:15 2014-10-02 04:21:11        2 IN      
 9 5004.24 RGD1  Striped Bass 2014-10-02 04:23:31 2014-10-02 04:26:06       NA IN      
10 5004.24 RGD1  Striped Bass 2014-10-02 04:28:21 2014-10-02 04:32:16       NA IN      
11 5004.24 RGD1  Striped Bass 2014-10-02 22:00:06 2014-10-02 22:44:08       NA IN      
12 5004.24 RGD1  Striped Bass 2014-10-02 22:46:58 2014-10-02 23:08:21        5 IN      
13 5004.24 RGD1  Striped Bass 2014-10-02 23:10:36 2014-10-03 00:26:00        5 IN      
14 5004.24 RGD1  Striped Bass 2014-10-03 00:28:55 2014-10-03 00:51:35        5 IN      
15 5004.24 RGD1  Striped Bass 2014-10-03 00:55:06 2014-10-03 01:08:01        5 IN      
16 5004.24 RGD1  Striped Bass 2014-10-03 01:10:36 2014-10-03 01:17:21        6 IN      
17 5004.24 RGD1  Striped Bass 2014-10-03 01:20:41 2014-10-03 01:21:01        6 IN      
18 5004.24 RGD1  Striped Bass 2014-10-03 01:30:41 2014-10-03 01:31:07        6 IN      
19 5004.24 RGD1  Striped Bass 2014-10-03 01:35:02 2014-10-03 01:38:12        7 IN      
20 5004.24 RGD1  Striped Bass 2014-10-03 01:42:02 2014-10-03 01:58:18        7 IN      

Некоторые обнаружения могут не происходить, когда ворота закрыты, поэтому «NA» действителен

Есть ли какие-нибудь лучшие варианты?

dput:

Обнаружения:

structure(list(Tag = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("5004.24", 
"5010.04", "5011.03", "5011.07", "5017.06", "5025.22", "5025.26", 
"5032.24", "5038.04", "5039.03", "5039.07", "5045", "5053.26", 
"5067.07", "5073.06", "5074.16", "5088.11", "5094.04", "5101.06", 
"5116.24", "5123.03", "5123.07", "5150.04", "5157.06", "5165.22", 
"5172.24", "5179.03", "5179.07", "5186.16", "5200.11", "5206.31", 
"5214.16", "5228.24", "5235", "5242.16", "5249", "5256.24", "5263.07", 
"5270.16", "5284.11", "5290.31", "5298.16", "5312.11", "5318.04", 
"5326.16", "5340.11", "5347.07", "5361.26", "5368.24", "5374.04", 
"5375.03", "5375.07", "5381.06", "5402.31", "5403.07", "5431.07", 
"5438.16", "5445.26", "5465.06", "5480.24", "5487.03", "5487.07", 
"5493.06", "5501.22", "5514.31", "5536.11", "5542.31", "5550.16", 
"5557.22", "5564.24", "5570.04", "5571.03", "5571.07", "5577", 
"5585.26", "5592.11", "5599.15", "5605.06", "5620.11", "5626.31", 
"5627.15", "5641.22", "5641.26", "5648.11", "5654.31", "5662.16", 
"5676.24", "5682.04", "5683.03", "5683.07", "5690.16", "5697.22", 
"5697.26", "5704.11", "5710.04", "5717.06", "5732.11", "5738.31", 
"5739.15", "5744.11", "5746.16", "5753", "5760.24", "5766.31", 
"5767.01", "5774.16", "5781.22", "5788.11", "5794.31", "5802.16", 
"5816.11", "5822.04", "5823.15", "5829.06", "5837.26", "5844.24", 
"5851.03", "5851.07", "5857.06", "5858.16", "5865.22", "5872.24", 
"5878.31", "5879.03", "5879.07", "5886.16", "5893.22", "5900.24", 
"5906.31", "5907.01", "5914.16", "5921.22", "5928.24", "5934.31", 
"5935.01", "5949.26", "5956.24", "5990.31", "5991.07", "5998.16", 
"6012.24", "6018.04", "6019.03", "6019.07", "6025.06", "6033.26", 
"6040.24", "6046.04", "6047.01", "6053", "6061", "6068.24", "6075.01", 
"6096.11", "6102.31", "6103.07", "6124.11", "6130.31", "6131.15", 
"6145.26", "6158.04", "6159.07", "6165.06", "6173.22", "6180.11", 
"6186.31", "6187.15", "6201.22", "6208.24", "6214.04", "6215.01", 
"6221.06", "6236.11", "6242.31", "6264.11", "6270.04", "6277.06", 
"6285.26", "6292.24", "6298.04", "6299.03", "6299.07", "6305.06", 
"6320.11", "6326.31", "6327.15", "6341.26", "6348.11", "6355.15", 
"6361.06", "6376.11", "6382.04", "6383.15", "6389.06", "6404.24", 
"6410.31", "6411.03", "6411.07", "6425.22", "6425.26", "6432.24", 
"6438.31", "6439.03", "6460.11", "6474.16", "6488.11", "6494.04", 
"6495.15", "6501.06", "6502.16", "6509.22", "6516.24", "6523.01", 
"6529.06", "6537.26", "6544.24", "6550.31", "6551.03", "6551.07", 
"6558.16", "6572.24", "6578.31", "6579.03", "6579.07", "6600.24", 
"6606.31", "6607.03", "6607.07", "6614.16", "6621.26", "6628.11", 
"6634.31", "6635.15", "6649.26", "6656.24", "6662.31", "6663.03", 
"6663.07", "6670.16", "6684.11", "6690.31", "6691.15", "6698.16", 
"6705.22", "6712.24", "6718.31", "6719.07", "6746.04", "6747.07", 
"6753.06", "6768.24", "6775.01", "6796.24", "6803.03", "6803.07", 
"6809.06", "6824.24", "6831.03", "6838.16", "6845", "6852.24", 
"6858.04", "6859.03", "6859.07", "6873.26", "6886.04", "6887.07", 
"6893.06", "6894.16", "6901.22", "6915.07", "6921.06", "6936.11", 
"6942.04", "6943.15", "6949.06", "6964.11", "6970.04", "6971.15", 
"6977.06", "6992.11", "6998.04", "6999.15", "7005.06", "7006.16", 
"7020.24", "7026.31", "7027.03", "7027.07", "7034.16", "7041.22", 
"7048.24", "7054.31", "7055.01", "7062.16", "7076.24", "7082.31", 
"7083.01", "7090.16", "7097", "7104.24", "7111.01", "7118.16", 
"7132.11", "7153.22", "7167.07", "7173.06", "7188.11", "7194.31", 
"7195.15", "7216.24", "7222.31", "7223.03", "7223.07", "7244.24", 
"7250.31", "7251.03", "7251.07", "7278.04", "7285.06", "7286.16", 
"7300.11", "7306.31", "7321.26", "7328.24", "7334.31", "7335.01", 
"7356.24", "7363", "7369.06", "7370.16", "7377.26", "7384.11", 
"7390.04", "7391.15", "7397.06", "7398.16", "7412.24", "7418.31", 
"7419.07", "7426.16", "7440.24", "7447.01", "7453.06", "7454.16", 
"7468.24", "7481.06", "7489", "7496.24", "7502.04", "7503.07", 
"7509", "7510.16", "7517.22", "7517.26", "7524.24", "7530.04", 
"7531.03", "7531.07", "7537.06", "7552.24", "7558.31", "7559.03", 
"7559.07", "7580.11", "7587.15", "7601.26", "7615.07", "7621.06", 
"7622.16", "7629.26", "7636.11", "7664.11", "7678.16", "7699.07", 
"7705.06", "7713.22", "7720.24", "7727.03", "7727.07", "7733.06", 
"7734.16", "7755.07", "7761.06", "7769.22", "7769.26", "7776.24", 
"7782.04", "7783.03", "7783.07", "7789.06", "7810.31", "7811.07", 
"7832.11", "7838.31", "7839.15", "7846.16", "7860.24", "7874.16", 
"7888.24", "7894.31", "7895.01", "7909.22", "7909.26", "7916.24", 
"7923", "7937", "7944.11", "7958.16", "7972.11", "7978.31", "7979.15", 
"8000.11", "8006.31", "8007.15", "8028.24", "8035.01", "8042.16", 
"8056.24", "8063.03", "8063.07", "8070.16", "8084.11", "8098.16", 
"8105.22", "8112.24", "8118.31", "8119.01", "8133.26", "8140.24", 
"8146.04", "8147.03", "8147.07", "8153.06", "8154.16", "8168.11", 
"8174.31", "8182.16", "8196.24", "8202.31", "8203.03", "8203.07", 
"8217.26", "8224.24", "8231.03", "8231.07", "8258.31", "8273.22", 
"8280.11", "8286.31", "8301.22", "8308.24", "8314.31", "8315.07", 
"8336.11", "8343.15", "8349.06", "8350.16", "8364.24", "8370.31", 
"8371.07", "8385", "8392.24", "8398.31", "8399.03", "8399.07", 
"8420.11", "8426.31", "8427.15", "8448.11", "8455.15", "8462.16", 
"8476.24", "8483.03", "8483.07", "8489.06", "8504.24", "8510.04", 
"8511.03", "8511.07", "8517.06", "8518.16", "8532.24", "8538.04", 
"8539.03", "8539.07", "8560.24", "8566.31", "8567.03", "8567.07", 
"8581.22", "8595.07", "8601.06", "8616.11", "8629.06", "8630.16", 
"8637.26", "8644.11", "8651.15", "8672.24", "8678.31", "8679.07", 
"8686.16", "8700.11", "8706.04", "8707.15", "8713.06", "8714.16", 
"8735.07", "8741.06", "8742.16", "8756.11", "8762.04", "8777.22", 
"8777.26", "8784.24", "8790.31", "8791.03", "8791.07", "8798.16", 
"8812.24", "8818.31", "8819.03", "8819.07", "8833.22", "8846.31", 
"8874.31", "8875.07", "8882.16", "8896.11", "8902.31", "8910.16", 
"8924.11", "8937.06", "8938.16", "8952.11", "8958.04", "8959.15", 
"8965.06", "8980.24", "8986.31", "8987.03", "8987.07", "8994.16", 
"9008.24", "9014.31", "9015.01", "9043.01", "9049.06", "9057.22", 
"9064.24", "9070.04", "9071.01", "9077.06", "9078.16", "9085.22", 
"9092.11", "9105.06", "9113.26", "9120.24", "9127.03", "9127.07", 
"9134.16", "9141.26", "9154.31", "9155.03", "9155.07", "9169.22", 
"9176.11", "9189.06", "9197", "9210.31", "9211.07", "9225", "9232.24", 
"9238.31", "9239.01", "9246.16", "9260.24", "9266.31", "9288.11", 
"9294.04", "9301.06", "9302.16", "9309.26", "9316.24", "9323.03", 
"9323.07", "9344.24", "9351.03", "9351.07", "9372.24", "9378.31", 
"9386.16", "9393.26", "9400.24", "9407.03", "9407.07", "9428.24", 
"9434.31", "9435.03", "9435.07", "9456.11", "9462.31", "9463.15", 
"9470.16", "9477.22", "9490.04", "9491.07", "9497.06", "9512.11", 
"9518.04", "9519.15", "9525.06", "9540.24", "9547.03", "9547.07", 
"9568.24", "9574.31", "9575.01", "9596.11", "9603.07", "9610.16", 
"9624.11", "9631.15", "9637.06", "9652.24", "9658.04", "9659.03", 
"9659.07", "9665.06", "9673.26", "9680.11", "9686.31", "9708.24", 
"9714.31", "9715.01", "9729.26", "9736.24", "9742.04", "9743.03", 
"9743.07", "9757.26", "9764.24", "9770.31", "9771.07", "9785.22", 
"9785.26", "9792.24", "9798.31", "9813.22", "9820.24", "9826.04", 
"9827.03", "9827.07", "9833.06", "9841.22", "9848.11", "9855.15", 
"9862.16", "9869.26", "9876.24", "9882.31", "9890.16", "9897.26", 
"9904.24", "9911.07", "9917.06", "9939.03", "9939.07", "9967.07", 
"9973.06", "9988.24", "9995.01"), class = "factor"), Start.Time = c("9/29/2014 10:40:40 PM", 
"9/29/2014 10:49:15 PM", "10/1/2014 11:01:12 PM", "10/1/2014 11:16:18 PM", 
"10/2/2014 12:15:47 AM", "10/2/2014 12:33:12 AM", "10/2/2014 1:13:01 AM", 
"10/2/2014 4:14:15 AM", "10/2/2014 4:23:31 AM", "10/2/2014 4:28:21 AM", 
"10/2/2014 10:00:06 PM", "10/2/2014 10:46:58 PM", "10/2/2014 11:10:36 PM", 
"10/3/2014 12:28:55 AM", "10/3/2014 12:55:06 AM", "10/3/2014 1:10:36 AM", 
"10/3/2014 1:20:41 AM", "10/3/2014 1:30:41 AM", "10/3/2014 1:35:02 AM", 
"10/3/2014 1:42:02 AM", "10/3/2014 2:05:05 AM", "10/3/2014 2:12:30 AM", 
"10/3/2014 2:17:05 AM", "10/3/2014 2:21:36 AM", "10/3/2014 2:28:01 AM", 
"10/3/2014 2:34:52 AM", "10/3/2014 4:01:03 AM", "10/3/2014 4:05:58 AM", 
"10/3/2014 4:18:34 AM", "10/3/2014 4:28:29 AM", "10/3/2014 4:31:50 AM", 
"10/3/2014 4:35:55 AM", "10/3/2014 6:00:15 AM", "10/3/2014 6:29:57 AM", 
"10/3/2014 6:33:37 AM", "10/3/2014 6:46:58 AM", "10/3/2014 7:02:00 AM", 
"10/3/2014 7:11:36 AM", "10/3/2014 7:18:32 AM", "10/3/2014 7:36:38 AM", 
"10/3/2014 8:04:27 AM", "10/3/2014 9:30:15 AM", "10/3/2014 10:16:38 AM", 
"10/3/2014 11:28:14 AM", "10/3/2014 12:13:46 PM", "10/3/2014 1:08:55 PM", 
"10/3/2014 1:12:10 PM", "10/3/2014 2:28:59 PM", "10/3/2014 2:51:51 PM", 
"10/3/2014 3:13:46 PM", "10/3/2014 3:43:47 PM", "10/3/2014 4:05:47 PM", 
"10/3/2014 5:12:46 PM", "10/3/2014 6:02:10 PM", "10/3/2014 6:25:01 PM", 
"10/3/2014 11:19:32 PM", "10/4/2014 9:16:18 AM", "10/4/2014 11:07:55 AM", 
"10/4/2014 11:59:58 AM", "10/4/2014 12:42:29 PM", "10/4/2014 2:00:54 PM", 
"10/4/2014 2:05:44 PM", "10/4/2014 2:09:39 PM", "10/4/2014 2:31:20 PM", 
"10/4/2014 5:20:04 PM", "10/4/2014 6:23:53 PM", "10/4/2014 6:31:39 PM", 
"10/4/2014 6:35:19 PM", "10/4/2014 6:38:40 PM", "10/4/2014 7:04:09 PM", 
"10/4/2014 7:16:35 PM", "10/5/2014 2:51:50 AM", "10/5/2014 3:08:26 AM", 
"10/5/2014 3:15:06 AM", "10/5/2014 3:46:12 AM", "10/5/2014 3:52:58 AM", 
"10/5/2014 4:00:58 AM", "10/5/2014 4:05:54 AM", "10/5/2014 4:27:50 AM", 
"10/5/2014 5:03:22 AM", "10/5/2014 5:09:42 AM", "10/5/2014 5:24:23 AM", 
"10/5/2014 5:35:29 AM", "10/5/2014 5:39:44 AM", "10/5/2014 6:00:40 AM", 
"10/5/2014 6:10:31 AM", "10/5/2014 6:18:22 AM", "10/5/2014 6:26:11 AM", 
"10/5/2014 6:33:52 AM", "10/5/2014 6:54:43 AM", "10/5/2014 7:11:13 AM", 
"10/5/2014 7:15:43 AM", "10/5/2014 8:08:37 AM", "10/5/2014 9:10:24 AM", 
"10/5/2014 9:13:14 AM", "10/5/2014 9:27:19 AM", "10/5/2014 9:40:26 AM", 
"10/5/2014 10:17:48 AM", "10/5/2014 10:51:59 AM", "10/5/2014 10:55:04 AM"
), total.duration = c(355L, 50L, 611L, 3419L, 861L, 2229L, 431L, 
416L, 155L, 235L, 2642L, 1283L, 4524L, 1360L, 775L, 405L, 20L, 
26L, 190L, 976L, 166L, 136L, 60L, 145L, 41L, 80L, 146L, 380L, 
201L, 41L, 80L, 461L, 752L, 45L, 670L, 85L, 426L, 286L, 935L, 
1256L, 4882L, 2644L, 4111L, 2212L, 256L, 50L, 720L, 1192L, 1085L, 
1515L, 931L, 3759L, 1851L, 1201L, 840L, 2397L, 6528L, 2993L, 
2421L, 3597L, 35L, 35L, 6L, 1327L, 246L, 221L, 65L, 25L, 830L, 
596L, 831L, 310L, 245L, 1696L, 100L, 336L, 85L, 1181L, 1847L, 
170L, 586L, 355L, 45L, 866L, 371L, 310L, 294L, 271L, 245L, 190L, 
26L, 3004L, 3517L, 35L, 706L, 641L, 2112L, 1892L, 6L, 71L), site = structure(c(4L, 
4L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L), .Label = c("CLRS", "CVP1", 
"GL1", "IC1", "IC2", "IC3", "ORN1", "ORN2", "ORS1", "ORS3", "RGD1", 
"RGU1", "WC1", "WC2", "WC3"), class = "factor"), number.Of.Pings = c(15L, 
4L, 25L, 513L, 163L, 368L, 27L, 16L, 10L, 14L, 214L, 167L, 566L, 
175L, 146L, 13L, 5L, 7L, 13L, 82L, 17L, 10L, 7L, 8L, 3L, 15L, 
10L, 32L, 10L, 4L, 8L, 47L, 44L, 8L, 75L, 10L, 20L, 31L, 47L, 
266L, 623L, 398L, 480L, 305L, 19L, 10L, 23L, 71L, 90L, 110L, 
108L, 797L, 167L, 105L, 118L, 211L, 773L, 368L, 455L, 729L, 8L, 
11L, 3L, 59L, 20L, 8L, 2L, 5L, 33L, 24L, 68L, 20L, 14L, 118L, 
2L, 19L, 12L, 85L, 117L, 8L, 33L, 13L, 9L, 85L, 42L, 19L, 11L, 
20L, 25L, 10L, 4L, 500L, 703L, 2L, 124L, 101L, 449L, 401L, 4L, 
9L), species = c("Striped Bass", "Striped Bass", "Striped Bass", 
"Striped Bass", "Striped Bass", "Striped Bass", "Striped Bass", 
"Striped Bass", "Striped Bass", "Striped Bass", "Striped Bass", 
"Striped Bass", "Striped Bass", "Striped Bass", "Striped Bass", 
"Striped Bass", "Striped Bass", "Striped Bass", "Striped Bass", 
"Striped Bass", "Striped Bass", "Striped Bass", "Striped Bass", 
"Striped Bass", "Striped Bass", "Striped Bass", "Striped Bass", 
"Striped Bass", "Striped Bass", "Striped Bass", "Striped Bass", 
"Striped Bass", "Striped Bass", "Striped Bass", "Striped Bass", 
"Striped Bass", "Striped Bass", "Striped Bass", "Striped Bass", 
"Striped Bass", "Striped Bass", "Striped Bass", "Striped Bass", 
"Striped Bass", "Striped Bass", "Striped Bass", "Striped Bass", 
"Striped Bass", "Striped Bass", "Striped Bass", "Striped Bass", 
"Striped Bass", "Striped Bass", "Striped Bass", "Striped Bass", 
"Striped Bass", "Striped Bass", "Striped Bass", "Striped Bass", 
"Striped Bass", "Striped Bass", "Striped Bass", "Striped Bass", 
"Striped Bass", "Striped Bass", "Striped Bass", "Striped Bass", 
"Striped Bass", "Striped Bass", "Striped Bass", "Striped Bass", 
"Striped Bass", "Striped Bass", "Striped Bass", "Striped Bass", 
"Striped Bass", "Striped Bass", "Striped Bass", "Striped Bass", 
"Striped Bass", "Striped Bass", "Striped Bass", "Striped Bass", 
"Striped Bass", "Striped Bass", "Striped Bass", "Striped Bass", 
"Striped Bass", "Striped Bass", "Striped Bass", "Striped Bass", 
"Striped Bass", "Striped Bass", "Striped Bass", "Striped Bass", 
"Striped Bass", "Striped Bass", "Striped Bass", "Striped Bass", 
"Striped Bass"), StartDateTime_UTC = structure(c(1412030440, 
1412030955, 1412204472, 1412205378, 1412208947, 1412209992, 1412212381, 
1412223255, 1412223811, 1412224101, 1412287206, 1412290018, 1412291436, 
1412296135, 1412297706, 1412298636, 1412299241, 1412299841, 1412300102, 
1412300522, 1412301905, 1412302350, 1412302625, 1412302896, 1412303281, 
1412303692, 1412308863, 1412309158, 1412309914, 1412310509, 1412310710, 
1412310955, 1412316015, 1412317797, 1412318017, 1412318818, 1412319720, 
1412320296, 1412320712, 1412321798, 1412323467, 1412328615, 1412331398, 
1412335694, 1412338426, 1412341735, 1412341930, 1412346539, 1412347911, 
1412349226, 1412351027, 1412352347, 1412356366, 1412359330, 1412360701, 
1412378372, 1412414178, 1412420875, 1412423998, 1412426549, 1412431254, 
1412431544, 1412431779, 1412433080, 1412443204, 1412447033, 1412447499, 
1412447719, 1412447920, 1412449449, 1412450195, 1412477510, 1412478506, 
1412478906, 1412480772, 1412481178, 1412481658, 1412481954, 1412483270, 
1412485402, 1412485782, 1412486663, 1412487329, 1412487584, 1412488840, 
1412489431, 1412489902, 1412490371, 1412490832, 1412492083, 1412493073, 
1412493343, 1412496517, 1412500224, 1412500394, 1412501239, 1412502026, 
1412504268, 1412506319, 1412506504), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), Duration_sec = new("Period", .Data = c(355, 
50, 611, 3419, 861, 2229, 431, 416, 155, 235, 2642, 1283, 4524, 
1360, 775, 405, 20, 26, 190, 976, 166, 136, 60, 145, 41, 80, 
146, 380, 201, 41, 80, 461, 752, 45, 670, 85, 426, 286, 935, 
1256, 4882, 2644, 4111, 2212, 256, 50, 720, 1192, 1085, 1515, 
931, 3759, 1851, 1201, 840, 2397, 6528, 2993, 2421, 3597, 35, 
35, 6, 1327, 246, 221, 65, 25, 830, 596, 831, 310, 245, 1696, 
100, 336, 85, 1181, 1847, 170, 586, 355, 45, 866, 371, 310, 294, 
271, 245, 190, 26, 3004, 3517, 35, 706, 641, 2112, 1892, 6, 71
), year = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
    month = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0), day = c(0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), hour = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0), minute = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), EndDateTime_UTC = structure(c(1412030795, 
1412031005, 1412205083, 1412208797, 1412209808, 1412212221, 1412212812, 
1412223671, 1412223966, 1412224336, 1412289848, 1412291301, 1412295960, 
1412297495, 1412298481, 1412299041, 1412299261, 1412299867, 1412300292, 
1412301498, 1412302071, 1412302486, 1412302685, 1412303041, 1412303322, 
1412303772, 1412309009, 1412309538, 1412310115, 1412310550, 1412310790, 
1412311416, 1412316767, 1412317842, 1412318687, 1412318903, 1412320146, 
1412320582, 1412321647, 1412323054, 1412328349, 1412331259, 1412335509, 
1412337906, 1412338682, 1412341785, 1412342650, 1412347731, 1412348996, 
1412350741, 1412351958, 1412356106, 1412358217, 1412360531, 1412361541, 
1412380769, 1412420706, 1412423868, 1412426419, 1412430146, 1412431289, 
1412431579, 1412431785, 1412434407, 1412443450, 1412447254, 1412447564, 
1412447744, 1412448750, 1412450045, 1412451026, 1412477820, 1412478751, 
1412480602, 1412480872, 1412481514, 1412481743, 1412483135, 1412485117, 
1412485572, 1412486368, 1412487018, 1412487374, 1412488450, 1412489211, 
1412489741, 1412490196, 1412490642, 1412491077, 1412492273, 1412493099, 
1412496347, 1412500034, 1412500259, 1412501100, 1412501880, 1412504138, 
1412506160, 1412506325, 1412506575), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), StartOpen = c(FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE), EndOpen = c(FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, 
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE
), location = c("IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", 
"IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", 
"IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", 
"IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", 
"IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", 
"IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", 
"IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", 
"IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", 
"IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", "IN", 
"IN", "IN", "IN", "IN")), row.names = c(NA, -100L), class = c("tbl_df", 
"tbl", "data.frame"))

Интервалы закрытия ворот

new("Interval", .Data = c(-81060, -117060, -59400, -16200, -76680, 
-51000, -81120), start = structure(c(1412238660, 1412362800, 
1412434800, 1412454600, 1412542980, 1412602200, 1412690400), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), tzone = "UTC")

1 Ответ

1 голос
/ 28 октября 2019

Таким образом, существуют некоторые проблемы с данными вашего примера, которые затрудняют ответ на ваш вопрос.

  1. Ваши данные об обнаружении в качестве примера включают в себя много дополнительной информации, которая не нужна для решения или понимания вашего вопроса.
  2. Ваши примеры интервалов задом наперед. (Они заканчиваются прежде, чем начнутся)
  3. Данные вашего примера не настроены в удобном формате (вы не включаете пакеты, необходимые для их чтения).

Тем не менее, давайте упростим и исправим эти проблемы:

library(tidyverse)  # needed because your data is in tibble format
library(lubridate)  # needed for time interval class used in your example

# I called your example data "Detections" and "Intervals", not importing them here

set.seed(914)
row.sample <- c(1,5,2, sample(1:100, 10)) # to include the samples not in the given intervals mixed with good values
use.cols <- c(1,4,6,7,9)  
Detections.use <- Detections[row.sample, use.cols]

Detections.use
# A tibble: 13 x 5
   Tag     site  species      StartDateTime_UTC   EndDateTime_UTC    
   <fct>   <fct> <chr>        <dttm>              <dttm>             
 1 5004.24 IC1   Striped Bass 2014-09-29 22:40:40 2014-09-29 22:46:35
 2 5004.24 RGD1  Striped Bass 2014-10-02 00:15:47 2014-10-02 00:30:08
 3 5004.24 IC1   Striped Bass 2014-09-29 22:49:15 2014-09-29 22:50:05
 4 5004.24 RGD1  Striped Bass 2014-10-04 18:38:40 2014-10-04 18:52:30
 5 5004.24 RGD1  Striped Bass 2014-10-05 07:15:43 2014-10-05 08:05:47
 6 5004.24 RGD1  Striped Bass 2014-10-03 02:21:36 2014-10-03 02:24:01
 7 5004.24 RGD1  Striped Bass 2014-10-03 12:13:46 2014-10-03 12:18:02
 8 5004.24 RGD1  Striped Bass 2014-10-03 09:30:15 2014-10-03 10:14:19
 9 5004.24 RGD1  Striped Bass 2014-10-04 14:09:39 2014-10-04 14:09:45
10 5004.24 RGD1  Striped Bass 2014-10-03 02:17:05 2014-10-03 02:18:05
11 5004.24 RGD1  Striped Bass 2014-10-05 03:08:26 2014-10-05 03:12:31
12 5004.24 RGD1  Striped Bass 2014-10-03 01:42:02 2014-10-03 01:58:18
13 5004.24 RGD1  Striped Bass 2014-10-02 00:33:12 2014-10-02 01:10:21


# Convert your intervals to a table, then correct them to make them positive time intevals
Interval.table <- tibble(Intervals, "ID" = seq(from = 1, to = length(Intervals))) %>%
      mutate(end_time = Intervals$start,
             start_time = end_time+Intervals$.Data,
             new_Interval = start_time %--% end_time) %>%
      select(ID, new_Interval)
Interval.table
# A tibble: 7 x 2
     ID new_Interval                                    
  <int> <Interval>                                      
1     1 2014-10-01 10:00:00 UTC--2014-10-02 08:31:00 UTC
2     2 2014-10-02 10:29:00 UTC--2014-10-03 19:00:00 UTC
3     3 2014-10-03 22:30:00 UTC--2014-10-04 15:00:00 UTC
4     4 2014-10-04 16:00:00 UTC--2014-10-04 20:30:00 UTC
5     5 2014-10-04 23:45:00 UTC--2014-10-05 21:03:00 UTC
6     6 2014-10-05 23:20:00 UTC--2014-10-06 13:30:00 UTC
7     7 2014-10-06 15:28:00 UTC--2014-10-07 14:00:00 UTC

Теперь, чтобы ответить на ваш вопрос Пакет lubridate включает в себя функцию %within%, которая проверяет, находится ли время в пределах временного интервала, но странным образом для функций tidyverseкажется, это не очень хорошо векторизовано. Я не смог выяснить, как заставить это работать в рамках типичного синтаксиса dplyr / tidyr, но используя цикл for, мы можем создать вектор, содержащий идентификатор интервала, который каждый раз подходит, и NA для тех, которые являютсяне входит ни в один из данных интегралов.

y <- c()
for (i in 1:length(Detections.use$StartDateTime_UTC)){
z<- which(Detections.use$StartDateTime_UTC[i] %within% Interval.table$new_Interval)
y[i] <- ifelse(isTRUE(z>0), z, NA)
}
Detections.use$Interval <- y
Detections.use
# A tibble: 13 x 6
   Tag     site  species      StartDateTime_UTC   EndDateTime_UTC     Interval
   <fct>   <fct> <chr>        <dttm>              <dttm>                 <int>
 1 5004.24 IC1   Striped Bass 2014-09-29 22:40:40 2014-09-29 22:46:35       NA
 2 5004.24 RGD1  Striped Bass 2014-10-02 00:15:47 2014-10-02 00:30:08        1
 3 5004.24 IC1   Striped Bass 2014-09-29 22:49:15 2014-09-29 22:50:05       NA
 4 5004.24 RGD1  Striped Bass 2014-10-04 18:38:40 2014-10-04 18:52:30        4
 5 5004.24 RGD1  Striped Bass 2014-10-05 07:15:43 2014-10-05 08:05:47        5
 6 5004.24 RGD1  Striped Bass 2014-10-03 02:21:36 2014-10-03 02:24:01        2
 7 5004.24 RGD1  Striped Bass 2014-10-03 12:13:46 2014-10-03 12:18:02        2
 8 5004.24 RGD1  Striped Bass 2014-10-03 09:30:15 2014-10-03 10:14:19        2
 9 5004.24 RGD1  Striped Bass 2014-10-04 14:09:39 2014-10-04 14:09:45        3
10 5004.24 RGD1  Striped Bass 2014-10-03 02:17:05 2014-10-03 02:18:05        2
11 5004.24 RGD1  Striped Bass 2014-10-05 03:08:26 2014-10-05 03:12:31        5
12 5004.24 RGD1  Striped Bass 2014-10-03 01:42:02 2014-10-03 01:58:18        2
13 5004.24 RGD1  Striped Bass 2014-10-02 00:33:12 2014-10-02 01:10:21        1

Отредактировано, чтобы добавить sapply решение ---

Это также можно сделать с помощью sapply, немного более плавным и не выходя изАртефакты:

which.join <- function(x, y) {
      z <- which(x %within% y)
      z <- ifelse(isTRUE(z > 0), z, NA)
      z
}

Detections.use$Interval <- sapply(Detections.use$StartDateTime_UTC, 
                             function(x) which.join(x,Interval.table$new_Interval)

Другим подходом может быть использование пакета fuzzyjoin, как показано здесь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...