I have two lists - test.segments
- which represents timestamps and the second - test.coords
- which represents lat/lon coordinates. The number of elements in each list is equal - so both should theoretically be easily combined.
> test.segments
[[1]]
[1] 4380.000 4388.125 4396.250 4404.375 4412.500 4420.625 4428.750 4436.875 4445.000
[[2]]
[1] 4448.000 4449.667 4451.333 4453.000
[[3]]
[1] 4696.000 4716.444 4736.889 4757.333 4777.778 4798.222 4818.667 4839.111 4859.556 4880.000
> test.coords
[[1]]
[[1]][[1]]
[[1]][[1]][[1]]
[,1] [,2]
[1,] 19.93882 50.06548
[2,] 19.93882 50.06548
[3,] 19.93881 50.06556
[4,] 19.93885 50.06561
[5,] 19.93885 50.06562
[6,] 19.93885 50.06561
[7,] 19.93885 50.06561
[8,] 19.93885 50.06561
[9,] 19.93885 50.06561
[[2]]
[[2]][[1]]
[[2]][[1]][[1]]
[,1] [,2]
[1,] 19.91947 50.07203
[2,] 19.91947 50.07203
[3,] 19.91947 50.07203
[4,] 19.91947 50.07203
[[3]]
[[3]][[1]]
[[3]][[1]][[1]]
[,1] [,2]
[1,] 19.93553 50.06572
[2,] 19.93554 50.06568
[3,] 19.93554 50.06568
[4,] 19.93551 50.06574
[5,] 19.93537 50.06576
[6,] 19.93403 50.06723
[7,] 19.93394 50.06734
[8,] 19.93393 50.06738
[9,] 19.93393 50.06738
[10,] 19.93393 50.06738
My goal is to combine merge both lists into a json format below (note: the key vendor
should always be 0) so that startTime
represents the first segment, endTime
represents the last segment, and segments
are an array of [lat, lon, corresponding segment]
I have tried using the jsonlite
library but this cuts off coordinates after 4 decimal points, and I am not sure how to combine both lists. Any suggestions would be appreciated.
[
{
"vendor": 0,
"startTime": 4380,
"endTime": 4445,
"segments": [
[19.93882, 50.06548, 4380],
[19.93882, 50.06548, 4388.125],
[19.93881, 50.06556, 4396.250],
[19.93885, 50.06561, 4404.375],
[19.93885, 50.06562, 4412.500],
[19.93885, 50.06561, 4420.625],
[19.93885, 50.06561, 4428.750],
[19.93885, 50.06561, 4436.875],
[19.93885, 50.06561, 4445]
]
},
{
"vendor": 0,
"startTime": 4448,
"endTime": 4453,
"segments": [
[19.91947, 50.07203, 4448],
[19.91947, 50.07203, 4449.667],
[19.91947, 50.07203, 4451.333],
[19.91947, 50.07203, 4453]
]
},
{
"vendor": 0,
"startTime": 4696,
"endTime": 4880,
"segments": [
[19.93553, 50.06572, 4696],
[19.93554, 50.06568, 4716.444],
[19.93554, 50.06568, 4736.889],
[19.93551, 50.06574, 4757.333],
[19.93537, 50.06576, 4777.778],
[19.93403, 50.06723, 4798.222],
[19.93394, 50.06734, 4818.667],
[19.93393, 50.06738, 4839.111],
[19.93393, 50.06738, 4859.556],
[19.93393, 50.06738, 4880]
]
}
]
Here is the data sample used above:
> dput(test.segments)
list(c(4380, 4388.125, 4396.25, 4404.375, 4412.5, 4420.625, 4428.75,
4436.875, 4445), c(4448, 4449.66666666667, 4451.33333333333,
4453), c(4696, 4716.44444444444, 4736.88888888889, 4757.33333333333,
4777.77777777778, 4798.22222222222, 4818.66666666667, 4839.11111111111,
4859.55555555556, 4880))
> dput(test.coords)
list(list(list(structure(c(19.9388183333333, 19.9388183333333,
19.93881, 19.938845, 19.9388483333333, 19.9388516666667, 19.9388516666667,
19.9388516666667, 19.9388516666667, 50.0654816666667, 50.0654816666667,
50.0655566666667, 50.0656116666667, 50.065615, 50.0656083333333,
50.0656066666667, 50.0656066666667, 50.0656066666667), .Dim = c(9L,
2L)))), list(list(structure(c(19.9194666666667, 19.9194666666667,
19.9194666666667, 19.9194666666667, 50.0720283333333, 50.0720283333333,
50.0720283333333, 50.0720283333333), .Dim = c(4L, 2L)))), list(
list(structure(c(19.9355333333333, 19.93554, 19.93554, 19.9355116666667,
19.9353733333333, 19.9340316666667, 19.9339433333333, 19.9339266666667,
19.9339266666667, 19.9339266666667, 50.06572, 50.06568, 50.06568,
50.0657416666667, 50.065765, 50.06723, 50.0673366666667,
50.0673833333333, 50.0673816666667, 50.0673816666667), .Dim = c(10L,
2L)))))