Вопрос

I have the following data its about 4000 rows

Case             MAC           time_t     Sensor
------         --------       ---------   --------
881            10:B0:A0        80           1
103            14:A1:FF        100          1
232            13:A2:AB        180          1
210            10:B2:C2        350          1
432            14:A1:FF        500          2
455            13:A2:AB        600          2
192            10:B2:C2        700          2
323            13:A2:AB        800          2

I need to create the following table matching MACs from sensor(1) with sensor (2) to calculate the travel times.

Case_1    Case_2    matched_MAC      time_t       travel_time_t2-t1
------    ------    -----------     ------     ----------------------
881        N/A      10:B0:A0          N/A                N/A
103        432      14:A1:FF          500           500-100 = 400
232        455      13:A2:AB          600                420
232        323      13:A2:AB          800                620
210        192      10:B2:C2          700                350
Это было полезно?

Решение

Try something like:

#Create some sample data
sample.data <- structure(list(Case = c(881L, 103L, 232L, 210L, 432L, 455L, 192L,
323L), MAC = c("10:B0:A0", "14:A1:FF", "13:A2:AB", "10:B2:C2",
"14:A1:FF", "13:A2:AB", "10:B2:C2", "13:A2:AB"), time_t = c(80L,
100L, 180L, 350L, 500L, 600L, 700L, 800L), Sensor = c(1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L)), .Names = c("Case", "MAC", "time_t",
"Sensor"), class = "data.frame", row.names = c(NA, -8L))

#Split data into sensor 1 and sensor 2, then merge when MAC values coincide
summary_data <- merge(sample.data[sample.data$Sensor==1,],sample.data[sample.data$Sensor==2,],by="MAC",all=TRUE)
#Calculate elapsed time
summary_data$diff <- summary_data$time_t.y - summary_data$time_t.x

Note that if you have multiple repeated MAC values for both sensors, this will create a row for each combination of sensor 1 and sensor 2 - check that that's what you want.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top