Nbclust throwing error - r

I am trying to run a dataframe of 9 columns and 1064 rows through a k-means clustering algorithm, but I get the following error:
Error in NbClust(df, min.nc = 2, max.nc = 15, method = "kmeans") :
The TSS matrix is indefinite. There must be too many missing values. The index cannot be calculated.
However, there are no missing values
> dim(df)
[1] 1064 9
> sum(is.na(df))
[1] 0
Any idea what the problem could be and how to resolve it?
> head(df)
hr_830 hr_930 hr_1030 hr_1130 hr_160 hr_180 hr_190 hr_200 hr_0
1 2 2 2 2 2 2 2 2 2
2 2 2 2 2 2 2 2 2 3
3 2 2 2 2 2 2 2 2 3
4 2 2 2 2 2 2 2 2 2
5 2 2 2 2 2 2 2 2 2
6 2 2 2 2 2 2 2 2 4
here is a sample of the input:
> dput(input)
structure(list(hr_830 = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L,
2L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), hr_930 = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 2L, 4L, 4L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), hr_1030 = c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 2L, 4L, 4L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), hr_1130 = c(2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L), hr_160 = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L), hr_180 = c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 2L, 4L, 4L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), hr_190 = c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 2L, 4L, 4L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), hr_200 = c(2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 4L, 2L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L), hr_0 = c(2L, 3L, 3L, 2L, 2L, 4L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 2L, 2L, 2L, 2L, 2L, 2L
)), .Names = c("hr_830", "hr_930", "hr_1030", "hr_1130", "hr_160",
"hr_180", "hr_190", "hr_200", "hr_0"), row.names = c(NA, 25L), class = "data.frame")

You may have too many constant attributes, too.
Most (all?) of these methods assume continuous values, not integers.

Related

Error: Argument 20 matches multiple formal arguments in venn.diagram function

I'm very new to the venn.diagram() function, and am trying to create a simple venn diagram. Here is the data I am using:
structure(list(Transmitter = c("1657", "1657", "1658", "1659",
"1659", "1660", "1660", "1661", "1662", "1663", "1663", "1664",
"1664", "1666", "1667", "1667", "1668", "1668", "1669", "1670",
"1671", "1671", "1672", "1672", "1673", "1673", "1674", "1674",
"1675", "1675", "1676", "1676", "1678", "1679", "1679", "1680",
"1681", "1681", "1682", "1682", "1683", "1684", "1685", "1686",
"1686", "9782", "9782", "24166", "24166", "24167", "24168", "24169",
"24170", "24171", "24172", "24173", "24174", "24175", "24175",
"24176", "24177", "24178", "24179", "24179", "24180", "24181",
"24182", "24183", "24184", "24184", "24185", "24186", "24187",
"24188", "24189", "24190", "24191", "24192", "24193", "24194",
"24194", "24195", "24195", "24196", "24197", "24198", "24198",
"24199", "24199", "24200", "24201", "24203", "24204", "24204",
"24206", "24207", "24209", "24210", "24211", "24212", "24212",
"24213", "24214", "24215", "24216", "24216", "24217", "24218",
"24219", "30759", "30760", "30761", "30761", "30761", "30762",
"30763", "30764", "30765", "30765", "30765", "30766", "30766",
"30766", "30767", "30767", "30768", "30768", "30768", "30769",
"30769", "30769", "30770", "30771", "30772", "30772", "30772",
"30773", "30773", "30773", "30774", "30774", "30775", "30775",
"30776", "30776", "30777", "30777", "30777", "30778", "30778",
"30779", "30780", "30780", "30780", "30781", "30782", "30782",
"30783", "30784", "30785", "30786", "30787", "30788", "30788"
), Direction = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 2L,
1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L,
2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L), .Label = c("Marine",
"River"), class = "factor")), row.names = c(NA, -164L), class = "data.frame")
I want to create a venn diagram with a circle for each direction. Inside each circle is a number indicating the number of transmitters that are considered 'river', 'marine' or both.
This is some code I modified from a website:
install.packages('VennDiagram')
library(VennDiagram)
venn.diagram(
x = list(
lasts2WOFD %>% filter(Direction == 'Marine') %>% select(Transmitter) %>% unlist() ,
lasts2WOFD %>% filter(Direction == 'River') %>% select(Transmitter) %>% unlist()
),
category.names = c("Marine" , "Fresh"),
filename = 'VennDiagram',
output = TRUE ,
imagetype="png" ,
height = 480 ,
width = 480 ,
resolution = 300,
compression = "lzw",
lwd = 1,
col=c("#440154ff", '#21908dff'),
fill = c(alpha("#440154ff",0.3), alpha('#21908dff',0.3)),
cex = 0.5,
fontfamily = "sans",
cat.cex = 0.3,
cat.default.pos = "outer",
cat.pos = c(-27, 27),
cat.dist = c(0.055, 0.055),
cat.fontfamily = "sans",
cat.col = c("#440154ff", '#21908dff'),
rotation = 1
)
When run, I get this error:
Error in VennDiagram::draw.pairwise.venn(area1 = length(x[[1]]), area2 = length(x[[2]]), :
argument 20 matches multiple formal arguments
Regarding your question, I had a look at the source code of VennDiagram and I saw that rotation is part of venn.diagram, but not of draw.pairwise.venn. The parameter gets passed but cannot be used. Simply remove rotation=1 and it should work.
I understand this does not answer your question, but I just wanted to let you know that you can get the diagram with other packages. My nVennR package can do that in a couple of steps. If your object is called lasts2WOFD,
>library(nVennR)
>myV <- plotVenn(list(River=subset(lasts2WOFD, Direction == "River")$Transmitter, Marine=subset(lasts2WOFD, Direction == "Marine")$Transmitter))
The result would be:
You can control the output as explained in the vignette. You can also export a vectorial svg file that you can edit afterwards.

Add a column with repeating values taken from another data.frame

everybody! I have the following data:
> dput(test)
structure(list(CNTRYID = 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, 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2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Czech Republic",
"Denmark", "France", "Greece", "Hungary", "Italy", "Lithuania",
"Netherlands", "Poland", "Slovak Republic", "Slovenia", "Spain",
"United Kingdom"), class = "factor"), Gender = structure(c(2L,
2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 1L,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 2L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L,
1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L,
2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L,
1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L,
1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L,
2L, 1L, 2L), .Label = c("Male", "Female"), class = "factor"),
PVLIT1 = c(341.73556, 327.75647, 333.99133, 299.23201, 274.26806,
330.20199, 290.3974, 245.60594, 305.04734, 322.12211, 264.19379,
283.34121, 286.62649, 301.27084, 204.67276, 339.7475, 350.74884,
300.41595, 338.76949, 340.92763, 323.86579, 320.03587, 276.43592,
307.12426, 290.60296, 322.23858, 299.0517, 328.35244, 267.82705,
245.1432, 322.43248, 352.37208, 275.75408, 317.75532, 332.75897,
321.23265, 315.338, 303.61999, 309.63271, 315.13702, 349.31912,
330.28415, 291.53027, 238.86247, 336.98836, 312.80759, 312.32254,
238.765, 268.14566, 326.59843, 298.99782, 372.8563, 314.59135,
267.68085, 358.00259, 293.74775, 257.99369, 352.46286, 277.74005,
340.21297, 343.90215, 365.2711, 298.95923, 347.24665, 288.41685,
267.28314, 299.62821, 288.50113, 346.33, 348.62584, 304.37408,
288.28595, 209.42411, 308.3979, 317.11152, 339.05234, 338.79844,
273.4862, 317.05362, 321.9683, 298.49502, 315.31917, 285.24075,
260.5942, 292.17127, 278.49578, 351.55043, 291.29336, 345.61643,
252.87387, 318.81766, 359.97134, 334.8103, 282.96817, 347.12409,
266.44512, 284.23384, 303.45264, 228.6119, 248.62507, 445.14543,
262.78094, 330.89695, 276.94543, 340.09174, 370.30567, 254.49489,
267.31821, 235.36262, 268.87887, 293.34609, 323.89632, 332.1124,
270.03193, 302.92495, 290.86264, 330.11303, 225.08377, 306.51173,
393.41635, 296.14069, 290.8399, 249.71853, 273.45073, 391.61765,
229.62298, 368.49384, 352.84141, 276.27584, 271.89748, 345.33152,
279.98931, 322.87181, 262.99515, 251.44921, 291.01078, 302.7094,
288.9321, 335.60061, 353.84158, 225.1918, 267.17191, 261.80763,
291.8125, 354.75112, 263.19101, 300.22572, 309.91597, 323.37047,
371.69579, 288.89204, 344.10129, 211.20385, 268.50285, 347.76109,
223.8669, 280.59382, 341.03251, 330.07897, 297.91223, 382.22934,
346.63016, 279.97218, 296.75031, 279.52446, 239.17915, 307.47267,
310.66321, 339.9095, 288.07157, 313.38334, 294.49606, 301.16477,
330.97285, 306.29201, 258.9723, 305.16945, 220.56537, 293.92923,
316.1579, 294.9318, 231.12861, 327.08462, 295.45707, 212.56843,
343.58854, 321.65331, 347.51206, 327.05139, 270.26912, 245.49499,
284.13733, 346.38385, 262.40319, 305.82358, 268.60343, 314.58164,
247.77339, 346.95623, 326.82632, 296.7529, 252.09381, 307.30177,
262.73407, 261.99723, 265.99006, 297.02533, 221.36089, 342.78459,
259.3387, 308.40893, 345.39162, 252.01223, 249.46163, 232.65253,
295.11341, 269.32542, 270.33917, 311.52152, 295.45142, 299.57599,
264.36488, 253.78787, 277.27982, 184.86342, 343.89483, 304.73262,
315.47804, 277.46603, 294.02264, 278.49927, 288.03195, 261.12088,
258.48768, 286.84427, 232.02021, 239.09253, 249.53217, 341.65779,
199.08805, 348.26952, 292.84918, 233.47102, 280.34833, 266.45888,
300.40607, 287.81728, 293.79174, 257.01624, 375.42354, 212.49236,
326.45861, 266.21237, 282.09158, 273.21422, 226.8083, 320.00889,
300.76454, 261.39701, 263.16525, 325.74916, 285.175, 297.7344,
266.98718, 287.35748, 295.33917, 310.84685, 235.88418, 335.12738,
338.0637, 263.95686, 341.24316, 344.17478, 277.19368, 292.86124,
306.71616, 302.24125, 292.9568, 327.03543, 273.10249, 246.17394,
329.32818, 289.45179, 329.58524, 316.57873, 254.53541, 307.42027,
340.46333, 260.72247, 177.1923, 298.05925, 229.80109, 299.93516,
298.97657, 296.32594, 331.81735, 335.73543, 348.6484, 224.40826,
339.58894, 230.45043, 297.69392, 250.64399, 216.37015, 248.48309,
315.3377, 259.91751, 343.96253, 238.19071, 268.7662, 195.30995,
295.58828, 282.90717, 257.23455, 312.86206, 255.33727, 293.08026,
321.61584, 352.64202, 331.90932, 291.72882, 301.87908, 299.83755,
276.29271, 288.31371, 173.19914, 235.98186, 267.68055, 273.57641,
259.38568, 282.82247, 255.2055, 259.92553, 324.37237, 323.27084,
297.1294, 293.29913, 319.26816, 296.19763, 287.20711, 120.7633,
299.14635, 324.02645, 272.24852, 280.46404, 363.56726, 213.18303,
318.84395, 307.23832, 337.49138, 352.61447, 295.99838, 245.2863,
303.07131, 200.14136, 314.2698, 290.86413, 289.6412, 232.13515,
328.96702, 250.57396, 251.36983, 259.23267, 277.98361, 306.61626,
298.90864, 336.54659, 270.34128, 295.23666, 289.28454, 314.25576,
305.90498, 223.59499, 297.3472, 302.97199, 306.82586, 293.8622,
277.52691, 274.99887, 309.28188, 243.9094, 287.04775, 332.49858,
316.85995, 227.84818, 338.34644, 231.03199, 280.42949, 326.23743,
287.90662, 308.44719, 224.65553, 255.51269, 320.01916, 284.76909,
310.07218, 305.64163, 267.90448, 291.91266, 311.95645, 234.77883,
288.47911, 337.66548, 235.67435, 328.26341, 217.05384, 271.18711,
256.92855, 306.35037, 298.57583, 220.25599, 305.70531, 397.23308,
243.34537, 318.81272, 287.84564, 291.19244, 260.50231, 309.29317,
288.32053, 285.65509, 270.71887, 349.55511, 266.04791, 305.22095,
259.29735, 283.99375, 281.30021, 368.59316, 300.66732, 253.21196,
277.61171, 313.79851, 310.08222, 288.8469, 329.40954, 324.01478,
283.84872, 293.73975, 175.58543, 287.87392, 304.40044, 294.23277,
268.17933, 309.02819, 253.19897, 265.68756, 290.41261, 333.8446,
248.33467, 298.56631, 196.18183, 249.01214, 239.07566, 272.0436,
266.38371, 244.71529, 230.62195, 280.98915, 327.92842, 291.64174,
290.58153, 278.05094, 242.61752, 290.2778, 349.53173, 206.13951,
293.64674, 201.95774, 353.00234, 301.41944, 266.58184, 313.45438,
290.98948, 287.79774, 334.35567, 276.27736, 271.85967, 233.80236,
275.925, 292.44499, 275.67027, 306.44843, 293.76468, 300.68248,
277.37136, 258.36308, 215.93238, 304.53151, 282.98405, 284.24563,
267.54523, 278.81698, 293.47491, 274.20848, 325.56625, 338.29286,
237.04901, 321.03634, 284.59905), PVLIT2 = c(329.96437, 309.7071,
323.76923, 311.6131, 296.0494, 320.40846, 316.35148, 284.25026,
305.10127, 317.73826, 279.00676, 311.32111, 263.20577, 295.35872,
247.77471, 312.83461, 338.78073, 306.69291, 323.277, 324.9266,
286.49722, 317.47413, 294.87445, 332.89844, 285.07891, 294.75732,
312.38012, 307.31727, 281.80926, 299.38988, 307.64408, 359.26622,
269.06506, 311.84518, 335.81958, 320.7753, 261.46395, 254.00522,
322.33784, 304.60692, 330.96759, 348.84054, 308.38673, 246.73035,
330.95077, 351.57083, 307.36752, 255.48044, 269.63236, 347.14345,
315.88441, 400.94726, 296.03271, 276.19477, 363.93767, 309.51741,
249.73923, 359.06376, 268.23778, 340.45045, 347.93015, 331.94389,
265.17793, 339.92872, 302.63463, 271.15349, 287.00579, 245.92875,
333.74443, 395.13695, 319.19868, 280.50088, 274.27643, 298.57925,
301.34704, 343.32807, 322.24002, 260.04914, 305.97704, 299.22391,
311.31108, 314.82843, 284.82475, 263.54816, 305.83341, 275.05002,
291.21003, 295.85667, 325.4418, 297.35548, 328.91424, 348.46255,
374.91294, 278.72346, 336.15884, 284.42308, 301.46666, 280.60555,
221.60585, 240.95952, 352.39105, 244.17594, 331.78841, 288.21081,
311.62633, 360.55586, 252.44882, 275.11625, 224.22946, 263.59549,
275.29046, 321.90792, 332.09749, 282.92641, 282.89338, 292.94236,
329.33191, 242.67135, 284.59736, 372.01791, 284.58945, 304.33581,
262.18477, 263.41397, 397.0077, 233.13107, 354.51546, 330.58595,
267.95011, 272.97652, 320.41706, 269.92821, 311.25004, 248.15808,
260.19179, 278.38501, 314.36786, 296.30594, 295.8064, 362.28497,
236.54304, 280.39167, 268.16006, 282.61124, 328.43024, 266.80519,
272.71165, 317.02057, 302.61842, 313.83703, 299.44275, 347.81048,
248.96742, 244.98621, 314.78989, 228.19594, 277.08832, 321.66255,
307.59243, 285.20515, 329.42832, 327.38645, 266.39655, 284.52323,
253.67779, 239.9784, 315.34247, 324.64586, 339.56843, 334.15503,
324.9195, 289.38614, 279.53691, 346.34106, 299.6327, 264.50415,
329.80362, 257.43317, 310.62546, 325.04705, 266.05947, 252.10474,
377.03047, 284.46253, 252.06975, 307.29189, 325.21219, 318.36119,
313.92387, 266.27629, 236.96554, 308.785, 327.27422, 260.62932,
298.31588, 266.69611, 343.64082, 249.20663, 368.61185, 314.38577,
296.44633, 301.93329, 305.82456, 234.26794, 285.09993, 264.74247,
291.16644, 261.86653, 326.92465, 271.35729, 298.4598, 315.83039,
251.59546, 248.09457, 250.90401, 313.28557, 281.10513, 289.41966,
322.99908, 308.48391, 303.46002, 259.34959, 285.41517, 276.54708,
215.90873, 320.46273, 307.92637, 329.45411, 280.43586, 319.99516,
295.35201, 270.81816, 260.6502, 277.0142, 285.64532, 217.5607,
245.98812, 226.29258, 309.23674, 220.03202, 387.01331, 268.84682,
248.05849, 279.63382, 285.34802, 284.09757, 273.3005, 259.08111,
264.10347, 386.52038, 210.39955, 320.20292, 245.43105, 301.72215,
262.78873, 207.51776, 329.13312, 321.57565, 258.19365, 291.23977,
310.97158, 272.66014, 259.28731, 246.29506, 319.95987, 294.63771,
311.32836, 312.20071, 350.12412, 308.50961, 237.55051, 343.6281,
340.66271, 297.44784, 331.91189, 276.27969, 301.48287, 321.79642,
285.85259, 256.98003, 267.14263, 334.84721, 348.23387, 322.91747,
268.42791, 269.33266, 334.05385, 319.43236, 270.38822, 168.24661,
327.46855, 248.39576, 294.05205, 318.50606, 312.98079, 287.35827,
361.43941, 342.54578, 247.997, 316.69669, 232.59609, 285.6277,
261.74517, 230.28962, 254.88764, 302.1326, 267.05801, 385.75184,
267.35567, 313.20042, 203.28131, 282.90967, 290.01701, 282.52713,
339.58608, 266.53576, 297.53657, 278.7528, 348.01977, 328.51913,
289.18677, 319.62583, 311.31862, 271.67348, 279.98333, 227.36405,
276.27979, 290.28081, 280.66062, 255.47229, 291.26037, 264.89911,
304.19635, 330.39094, 334.60314, 272.9507, 294.44286, 346.35474,
332.11805, 313.19078, 104.71963, 293.40003, 329.98007, 247.70109,
259.13664, 325.72004, 239.1354, 294.74629, 291.78074, 328.38065,
334.70175, 312.16118, 215.75044, 297.27003, 202.36576, 271.84798,
291.59486, 303.22928, 221.18819, 315.37282, 238.00923, 233.12403,
269.9957, 259.90847, 311.6859, 272.51505, 346.11677, 260.70023,
275.42651, 290.61255, 307.04942, 323.60677, 219.96542, 305.55666,
283.99912, 267.52644, 285.16624, 301.02858, 260.05581, 283.41617,
244.09153, 290.04512, 353.63745, 317.37715, 241.34487, 321.14259,
258.36885, 261.37509, 348.62232, 300.73864, 327.22453, 228.55662,
268.91226, 314.09536, 260.56452, 283.06898, 319.64052, 275.96927,
296.46456, 295.56963, 276.68877, 289.44363, 296.45405, 240.71778,
310.15017, 237.00255, 240.61887, 244.14616, 316.83501, 253.25562,
230.76127, 304.57009, 364.11619, 267.0067, 308.55546, 275.38505,
293.96026, 243.73364, 321.01363, 316.79925, 277.67496, 289.51552,
307.85001, 279.01546, 317.36872, 242.91248, 279.70631, 226.23475,
386.36518, 326.81052, 235.98733, 288.59577, 305.13604, 353.10836,
274.67248, 327.51103, 322.45477, 315.17773, 316.73504, 189.05388,
267.47862, 316.6865, 272.59329, 281.07099, 327.16369, 280.24953,
253.59041, 314.05915, 341.81589, 286.40082, 323.2246, 218.71734,
256.56632, 239.05101, 292.73646, 262.55946, 280.23444, 219.2759,
293.2041, 342.50186, 251.37571, 321.82169, 290.32648, 233.07586,
293.06438, 333.05446, 222.39499, 298.17628, 201.93622, 354.07415,
294.31312, 301.73295, 328.2708, 278.76013, 289.11145, 313.0081,
275.55262, 287.71408, 272.14237, 300.88391, 308.66827, 284.93954,
312.92511, 335.31426, 276.17927, 320.79382, 273.71349, 255.89581,
300.24576, 292.25961, 288.96434, 303.44888, 253.07684, 248.08721,
281.36322, 326.35893, 335.85343, 253.90892, 328.49694, 272.70062
), PVLIT3 = c(347.03202, 297.99638, 308.9164, 300.68665,
302.42232, 318.3244, 315.16283, 273.26457, 306.98393, 342.15998,
271.74761, 330.01561, 239.3113, 292.38404, 220.32939, 320.98693,
328.77846, 312.25874, 320.16997, 315.01401, 278.54675, 297.93796,
289.05429, 306.41476, 292.88945, 287.35494, 289.7173, 327.61128,
256.59233, 270.40046, 299.88817, 347.61574, 271.24062, 300.58625,
389.47902, 323.26406, 303.8882, 287.85783, 320.78071, 260.5962,
329.16759, 322.44865, 288.50371, 230.97053, 341.00067, 317.60536,
334.04519, 242.21659, 273.88791, 319.75284, 303.14072, 369.25668,
315.38962, 238.82989, 389.86658, 291.40597, 256.2491, 328.59089,
270.08341, 346.88612, 351.05896, 313.98005, 276.49836, 341.01902,
304.44641, 279.94384, 297.38058, 260.15889, 301.88881, 385.34273,
281.3716, 305.96796, 235.56287, 313.06217, 242.71572, 354.28779,
305.7085, 256.87025, 288.9405, 291.11581, 307.62043, 321.73213,
283.82863, 248.89211, 291.42861, 273.85537, 307.50928, 316.5518,
321.39642, 289.61352, 319.72759, 339.1263, 364.61416, 277.72976,
295.74977, 274.09616, 272.9924, 272.49014, 251.05889, 251.01214,
386.34787, 245.74296, 303.75875, 284.2808, 350.74965, 354.9783,
229.63546, 282.53014, 233.23552, 252.86831, 264.47331, 320.17187,
357.86769, 281.28431, 291.52807, 309.28932, 335.37317, 250.08053,
279.77425, 370.20707, 313.44683, 293.07481, 250.40199, 270.51998,
391.40329, 234.55338, 376.94785, 332.07567, 246.3079, 299.29627,
313.21714, 285.05673, 285.967, 272.23878, 254.95618, 291.76139,
311.07348, 291.55214, 330.95787, 368.524, 230.85077, 259.26676,
269.73751, 284.4568, 331.74769, 267.65832, 297.71321, 327.10192,
281.02533, 317.82234, 277.43442, 347.61242, 242.73154, 273.46411,
326.03792, 228.83492, 292.92737, 304.29623, 275.46119, 298.80275,
343.8432, 343.49854, 280.61476, 290.29079, 251.7561, 249.89728,
297.3227, 298.18224, 355.51642, 310.46884, 337.82139, 307.61772,
313.36828, 334.1722, 305.46306, 278.88311, 278.72036, 267.1024,
320.97308, 343.18164, 263.62036, 256.22528, 309.85014, 288.59665,
235.3395, 314.77392, 321.25776, 300.35306, 371.13057, 259.11446,
258.48696, 306.43744, 323.78058, 280.04595, 297.34385, 263.67336,
329.24681, 233.97186, 346.51321, 286.97003, 306.86708, 297.15846,
337.82102, 262.12682, 291.70902, 281.91251, 297.9659, 217.23621,
359.04224, 265.45122, 324.9777, 334.42103, 237.77726, 228.74243,
252.22045, 290.07852, 283.7953, 270.72377, 295.45991, 302.80527,
304.19675, 268.4273, 244.94332, 263.11038, 207.48832, 326.08576,
312.77998, 318.79277, 244.6571, 332.0414, 259.87963, 296.28998,
239.8572, 248.63712, 312.60629, 247.72389, 242.67078, 271.98961,
322.73757, 197.99485, 342.45941, 246.40288, 246.54248, 270.31797,
279.19136, 283.27782, 256.21676, 298.22446, 245.34207, 363.90223,
206.35297, 297.69943, 250.76662, 299.09484, 296.26682, 217.5044,
323.62796, 290.34726, 246.24697, 290.60326, 313.243, 254.3545,
273.66298, 265.30058, 304.26679, 309.53028, 310.93268, 318.41836,
318.59416, 344.05085, 247.21871, 319.13724, 314.71633, 262.51803,
277.58658, 240.33522, 283.6671, 315.50305, 284.70172, 260.21957,
247.34008, 347.11825, 315.84204, 310.46298, 278.06065, 264.27623,
336.07573, 326.01364, 245.67, 149.19113, 298.48698, 222.68479,
303.39236, 305.14083, 303.49802, 261.84537, 341.96969, 334.29589,
221.43909, 307.37339, 258.84784, 319.13495, 241.58692, 239.09068,
242.53418, 330.78695, 280.48138, 310.41672, 263.1502, 267.58253,
187.53822, 262.62308, 295.44728, 266.01227, 313.69862, 253.52788,
256.41986, 323.6009, 337.53842, 333.99872, 303.51809, 312.28169,
294.92838, 300.70826, 300.24336, 187.73782, 276.88791, 286.49432,
292.1261, 254.85939, 277.5201, 243.95193, 251.41231, 300.05949,
307.55581, 308.63016, 292.26692, 322.59732, 314.09203, 279.01204,
104.22403, 282.82249, 310.19937, 265.4532, 298.59688, 327.82834,
216.9004, 304.42974, 308.75751, 353.56875, 323.77259, 301.02944,
243.21455, 299.93867, 239.32609, 288.85644, 321.70498, 300.7652,
214.00534, 330.58371, 218.42779, 226.38351, 269.54573, 281.0985,
313.38796, 315.12442, 338.55295, 287.76669, 276.50012, 283.05571,
293.54309, 313.50773, 251.84096, 306.30878, 298.6039, 282.81368,
280.57491, 246.21204, 228.88241, 278.85273, 250.53517, 308.06586,
337.51522, 322.46427, 249.82595, 326.33555, 223.46005, 292.20952,
326.24258, 284.81759, 361.99513, 225.74007, 265.54327, 301.95898,
289.31911, 306.88864, 312.2168, 227.28386, 288.40244, 274.2207,
281.3788, 295.98457, 291.69465, 233.11237, 327.41621, 238.52888,
246.48451, 272.98087, 300.54158, 265.74598, 205.12706, 296.53577,
350.87861, 247.59947, 315.72462, 285.2722, 269.48033, 243.84168,
283.6546, 275.47676, 290.5681, 252.84893, 341.91242, 268.79573,
304.94915, 251.56578, 263.95641, 243.55776, 338.59661, 303.00255,
212.9379, 292.68837, 294.13636, 388.07221, 303.7387, 313.72581,
355.13609, 317.09882, 331.7978, 218.43844, 262.96991, 306.87193,
268.31627, 265.33588, 320.67174, 247.42967, 276.86054, 273.34922,
348.14517, 267.12308, 306.68901, 177.57379, 242.70165, 216.52883,
255.14138, 224.94714, 252.34893, 235.99393, 311.64778, 336.18343,
287.39269, 336.26041, 289.42778, 192.66316, 276.65478, 306.98647,
198.9643, 302.39132, 182.62885, 346.2712, 309.66977, 264.90717,
302.69623, 264.56688, 277.51619, 339.16197, 280.54847, 277.54282,
283.37459, 292.15455, 292.34224, 252.94767, 301.60766, 301.46605,
289.27154, 286.94408, 288.05704, 233.64855, 304.36141, 278.99481,
260.61019, 257.88718, 279.99577, 269.7465, 293.24509, 323.32979,
315.02618, 246.47989, 327.3044, 274.86892)), row.names = c(29L,
47L, 59L, 116L, 125L, 155L, 238L, 245L, 335L, 354L, 393L, 410L,
420L, 443L, 478L, 514L, 584L, 665L, 676L, 720L, 737L, 766L, 782L,
839L, 840L, 842L, 857L, 866L, 885L, 887L, 1017L, 1041L, 1083L,
1086L, 1153L, 1207L, 1219L, 1231L, 1276L, 1283L, 1389L, 1405L,
1423L, 1469L, 1517L, 1525L, 1544L, 1686L, 1766L, 1769L, 1773L,
1841L, 1854L, 1885L, 1925L, 1932L, 1956L, 1992L, 2067L, 2094L,
2105L, 2169L, 2262L, 2291L, 2293L, 2310L, 2349L, 2353L, 2358L,
2385L, 2441L, 2456L, 2490L, 2516L, 2545L, 2552L, 2557L, 2676L,
2683L, 2702L, 2733L, 2761L, 2801L, 2851L, 2876L, 2881L, 2882L,
2913L, 2927L, 2928L, 2991L, 3001L, 3018L, 3042L, 3071L, 3209L,
3239L, 3296L, 3298L, 3371L, 3502L, 3546L, 3559L, 3560L, 3561L,
3566L, 3589L, 3659L, 3669L, 3714L, 3717L, 3738L, 3744L, 3765L,
3768L, 3778L, 3782L, 3826L, 3840L, 3842L, 3848L, 3894L, 3968L,
3972L, 3984L, 4007L, 4064L, 4103L, 4160L, 4209L, 4212L, 4337L,
4377L, 4432L, 4671L, 4674L, 4675L, 4686L, 4714L, 4724L, 4761L,
4773L, 4853L, 4871L, 4908L, 4931L, 4993L, 5000L, 5039L, 5055L,
5081L, 5100L, 5173L, 5260L, 5265L, 5281L, 5331L, 5368L, 5412L,
5419L, 5435L, 5476L, 5482L, 5485L, 5500L, 5561L, 5591L, 5618L,
5629L, 5641L, 5745L, 5767L, 5783L, 5833L, 5876L, 5884L, 5933L,
5938L, 5943L, 5960L, 5997L, 6000L, 6016L, 6021L, 6109L, 6116L,
6124L, 6135L, 6157L, 6166L, 6172L, 6173L, 6176L, 6181L, 6214L,
6216L, 6226L, 6229L, 6264L, 6280L, 6282L, 6315L, 6330L, 6358L,
6366L, 6368L, 6375L, 6397L, 6463L, 6541L, 6543L, 6598L, 6604L,
6644L, 6649L, 6669L, 6690L, 6692L, 6702L, 6711L, 6716L, 6718L,
6725L, 6740L, 6769L, 6775L, 6781L, 6829L, 6841L, 6853L, 6860L,
6872L, 6890L, 6891L, 6902L, 6905L, 6913L, 6914L, 6927L, 6934L,
6937L, 6947L, 6965L, 6980L, 6981L, 7021L, 7072L, 7094L, 7104L,
7121L, 7136L, 7138L, 7148L, 7149L, 7168L, 7169L, 7222L, 7258L,
7263L, 7266L, 7292L, 7347L, 7350L, 7360L, 7382L, 7390L, 7419L,
7423L, 7455L, 7463L, 7471L, 7482L, 7509L, 7528L, 7543L, 7577L,
7583L, 7587L, 7593L, 7615L, 7616L, 7617L, 7633L, 7650L, 7654L,
7674L, 7688L, 7706L, 7718L, 7723L, 7748L, 7766L, 7797L, 7811L,
7813L, 7820L, 7839L, 7893L, 7899L, 7910L, 7926L, 7944L, 7945L,
7947L, 7954L, 7955L, 7959L, 7982L, 7990L, 8025L, 8031L, 8041L,
8044L, 8049L, 8062L, 8069L, 8072L, 8085L, 8090L, 8110L, 8122L,
8134L, 8141L, 8193L, 8196L, 8201L, 8203L, 8208L, 8233L, 8234L,
8236L, 8271L, 8273L, 8276L, 8284L, 8307L, 8328L, 8333L, 8354L,
8367L, 8386L, 8412L, 8421L, 8441L, 8452L, 8478L, 8488L, 8504L,
8509L, 8518L, 8534L, 8548L, 8558L, 8574L, 8578L, 8585L, 8620L,
8654L, 8660L, 8662L, 8663L, 8704L, 8707L, 8733L, 8751L, 8757L,
8817L, 8822L, 8824L, 8831L, 8840L, 8841L, 8851L, 8858L, 8877L,
8904L, 8919L, 8944L, 8945L, 8951L, 8954L, 8959L, 8966L, 8986L,
9049L, 9058L, 9075L, 9088L, 9111L, 9124L, 9132L, 9138L, 9148L,
9171L, 9183L, 9188L, 9207L, 9257L, 9280L, 9284L, 9288L, 9292L,
9316L, 9335L, 9341L, 9354L, 9392L, 9428L, 9439L, 9451L, 9483L,
9484L, 9493L, 9498L, 9532L, 9542L, 9566L, 9569L, 9582L, 9592L,
9613L, 9628L, 9634L, 9643L, 9647L, 9659L, 9665L, 9687L, 9697L,
9708L, 9713L, 9762L, 9773L, 9815L, 9825L, 9829L, 9830L, 9839L,
9842L, 9848L, 9854L, 9865L, 9868L, 9891L, 9896L, 9925L, 9945L,
10012L, 10023L, 10035L, 10046L, 10059L, 10067L, 10096L, 10105L,
10106L, 10137L, 10146L, 10154L, 10173L, 10178L, 10195L, 10201L,
10202L, 10233L, 10243L, 10259L, 10273L, 10277L, 10287L, 10288L,
10315L, 10316L, 10342L, 10345L, 10348L, 10354L, 10361L, 10372L,
10417L, 10420L, 10430L, 10454L, 10476L, 10477L, 10490L, 10498L,
10499L, 10532L, 10533L, 10550L, 10553L, 10557L, 10560L, 10572L,
10616L, 10663L, 10667L, 10687L, 10712L), class = "data.frame")
and I want to add another column here with values taken from another data. The other data is this one:
> dput(new)
structure(list(CNTRYID = structure(1:2, .Label = c("Czech Republic",
"Denmark", "France", "Greece", "Hungary", "Italy", "Lithuania",
"Netherlands", "Poland", "Slovak Republic", "Slovenia", "Spain",
"United Kingdom"), class = "factor"), Freq = c(184, 518), Mean = c(299.71,
294.09), s.e. = c(4.39, 2.28), SD = c(36.73, 44.8), s.e = c(3.4,
2.12)), row.names = 1:2, class = c("intsvy.mean", "data.frame"
))
What I want to do is to take the value under Mean from the second data.frame that is associated with, e.g. the Czech Republic, and put it in the first data.frame so that everywhere where CNTRYID is Czech Republic the value 299.71 to appear in a new column, say, test$MEAN. Then 294.09 to appear in every row where the CNTRYID is Denmark and so on. You might require package "intsvy" since one of the classes here is "intsvy.mean". I have extracted this from a large dataset I am working with, so there might be some additional labels etc. but, overall, you should be able to reproduce it. Thank you in advance!
You are looking for merge.
result <- merge(test,new,by="CNTRYID")

summarise with dplyr outputting only one value

I have a dataset like so:
> dput(data_melt)
structure(list(Compound = structure(c(2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Compd1", "Compound1"
), class = "factor"), Concentration = structure(c(5L, 1L, 2L,
3L, 4L, 5L, 1L, 2L, 3L, 4L,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, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), class = "factor", .Label = c("Tau1",
"Tau2", "Peak.Mean", "Area.Mean", "SS1.Mean")), value = c(1.38196479885153,
1.14405508500302, 0.988961970528585, 1.44849126088629, 0.492462666110073,
NA, 2.17712741416582, 1.67028746892543, 1.96489211952819, 1.2460774224718,
NA, 0.485501088636007, 0.580120526488655, 0.530538989313623,
0.884536240505712, 0.322958355856638, 0.740882962734369, 1.18088839355135,
1.48745421674211, 1.16792544841743, 1.11656132754921, 1.14457816659658,
0.0675070264176897, 0.176054869732887, 0.174862277854592, 0.200470189214318,
0.187717771153427, 0.181176140081454, 0.117339926372974, 0.0941816692818621,
0.156408537242293, 0.171156092362873, 0.0642141717879837, 0.107013341555486,
0.0892122245482354, 0.151976744172333, 0.198474636073771, 0.188703600586299,
0.10970902239241, 0.117358989261514, 0.100312892958432, 0.118208485589655,
0.154895187369863, 0.101035151359696, -3926.26508451201, -696.475731092535,
-4384.77847338655, -718.718487256701, -3164.8941685203, -818.006663108841,
-4658.25223372398, -826.496302684798, -2416.89272653148, -2558.96929067338,
-672.257745869921, -3996.85447223941, -706.5215296652, -4190.52281192937,
-726.870892539311, -2852.22943401345, -831.88857277573, -4580.4780146496,
-816.712564805672, -6189.10619924791, -2540.95473989213, -713.422629648631,
7712.37025286162, 1906.38208801373, 7583.22998649368, 1879.68741296455,
7424.47445663593, 2236.40541039894, 6178.69685860507, 2064.41869983299,
5953.06397562968, 6373.90332689516, 1813.36551434687, 4585.08608292281,
1524.44544360278, 4343.20955707026, 1547.28354007935, 4772.3012092321,
1889.5819203618, 4251.33850498831, 1687.78145119834, 5978.28926211454,
3879.07376129486, 1533.61842684178, -622.178041494169, -301.118488704851,
-618.132026278872, -302.614140229218, -821.035687044046, -383.60893819189,
-683.302506820162, -331.474546574133, -764.139865695781, -546.931098421476,
-300.006976301825, -622.178041494169, -301.118488704851, -618.132026278872,
-302.614140229218, -821.035687044046, -383.60893819189, -683.302506820162,
-331.474546574133, -764.139865695781, -546.931098421476, -300.006976301825
)), .Names = c("Compound", "Concentration", "Co.Agonist", "variable",
"value"), row.names = c(NA, -110L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x0000000000120788>)
>
My goal is to summarize the value column by the preceding columns (Compound name, Concentration, Co.Agonist, and variable.
I've tried this:
DatAgg = data_melt %>% group_by(Concentration,Co.Agonist,variable)%>%
summarise(mean=mean(data_melt$value,na.rm=TRUE),sd=sd(data_melt$value))
but the output doesn't collapse the data.frame to only the unique combinations and it only returns one value for all the variables.
I'm looking to use this dplyr solution to get mean and sd for each unique combination of the group_by variables.
The syntax should be
data_melt %>%
group_by(Concentration,Co.Agonist,variable) %>%
summarise(Mean = mean(value, na.rm=TRUE),
Sd = sd(value, na.rm=TRUE))
In the OP's code, it was taking the mean of the whole 'value' column (mean(data_melt$value, na.rm=TRUE)) after grouping by the variables. So, there will be a single unique value.
As the 'data_melt' is a data.table object, data.table methods can be used as well.
data_melt[, .(Mean = mean(value, na.rm=TRUE), Sd = sd(value, na.rm=TRUE)),
by = .(Concentration, Co.Agonist, variable)]

Looping through class ltraj?

Apologies if this is not the best forum to inquire this question.
Has anyone been able to loop/iterate though multiple GPS collared individuals that are in class ltraj (adehabitatlt)?
I've been trying to calculate Prox (https://cran.r-project.org/web/packages/wildlifeDI/vignettes/wildlifeDI-vignette.pdf) for multiple individuals but am struggling with how exactly to loop class ltraj because it's different than dataframe (which I'm accustomed to).
Thanks in advance.
install.packages('wildlifeDI', dependencies=TRUE)
library(wildlifeDI)
library(adehabitatLT)
chupacabra <- structure(list(CollarID = 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, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L), .Label = c("A4116F", "A4117M", "A4118F"), class = "factor"),
DateTime = structure(c(1433653200, 1433667600, 1433682060,
1433682300, 1433682600, 1433682900, 1433683200, 1433683500,
1433683800, 1433684100, 1433684400, 1433684700, 1433685000,
1433685300, 1433685600, 1433685900, 1433686200, 1433686500,
1433686800, 1433687100, 1433687400, 1433687700, 1433688000,
1433688300, 1433688600, 1433688900, 1433689200, 1433689500,
1433689800, 1433690100, 1433690400, 1433690700, 1433691000,
1433691300, 1433691600, 1433691900, 1433692200, 1433692500,
1433692800, 1433693100, 1433693400, 1433693700, 1433694000,
1433694300, 1433694600, 1433694900, 1433695200, 1433695500,
1433695800, 1433696100, 1433696400, 1433710860, 1433714400,
1433714700, 1433715000, 1433715300, 1433715600, 1433715900,
1433716200, 1433716500, 1433716800, 1433717100, 1433717400,
1433717700, 1433718000, 1433718300, 1433718600, 1433718900,
1433719200, 1433719500, 1433719800, 1433720100, 1433720400,
1433720700, 1433721000, 1433721300, 1433721600, 1433721900,
1433722200, 1433722500, 1433722800, 1433723100, 1433723400,
1433723700, 1433724060, 1433724300, 1433724600, 1433724900,
1433725200, 1433653200, 1433667660, 1433682060, 1433682300,
1433682600, 1433682900, 1433683200, 1433683500, 1433683800,
1433684100, 1433684400, 1433684700, 1433685000, 1433685300,
1433685660, 1433685900, 1433686200, 1433686500, 1433686800,
1433687100, 1433687400, 1433687700, 1433688000, 1433688300,
1433688660, 1433688900, 1433689200, 1433689500, 1433689800,
1433690100, 1433690400, 1433690700, 1433691000, 1433691300,
1433691600, 1433691900, 1433692200, 1433692500, 1433692800,
1433693100, 1433693400, 1433693700, 1433694060, 1433694300,
1433694600, 1433694900, 1433695200, 1433695500, 1433695800,
1433696100, 1433696400, 1433710860, 1433714400, 1433714700,
1433715000, 1433715300, 1433715600, 1433715900, 1433716200,
1433716500, 1433716800, 1433717100, 1433717400, 1433717700,
1433718000, 1433718300, 1433718600, 1433718900, 1433719200,
1433719500, 1433719800, 1433720100, 1433720400, 1433720700,
1433721000, 1433721300, 1433721600, 1433721900, 1433722200,
1433722500, 1433722800, 1433723100, 1433723400, 1433723700,
1433724000, 1433724300, 1433724600, 1433724900, 1433725200,
1433653249, 1433667666, 1433682089, 1433682349, 1433682632,
1433682936, 1433683234, 1433683536, 1433683837, 1433684144,
1433684443, 1433684739, 1433685031, 1433685370, 1433685634,
1433685935, 1433686236, 1433686536, 1433686826, 1433687142,
1433687448, 1433687736, 1433688034, 1433688337, 1433688649,
1433688936, 1433689236, 1433689531, 1433689827, 1433690139,
1433690433, 1433690736, 1433691048, 1433691336, 1433691634,
1433691941, 1433692236, 1433692535, 1433692833, 1433693129,
1433693434, 1433693735, 1433694028, 1433694373, 1433694642,
1433694931, 1433695234, 1433695542, 1433695831, 1433696148,
1433696448, 1433710908, 1433714437, 1433714737, 1433715036,
1433715366, 1433715636, 1433715969, 1433716234, 1433716536,
1433716827, 1433717137, 1433717435, 1433717733, 1433718048,
1433718336, 1433718636, 1433718973, 1433719272, 1433719530,
1433719837, 1433720136, 1433720431, 1433720736, 1433721031,
1433721336, 1433721640, 1433721946, 1433722236, 1433722528,
1433722842, 1433723137, 1433723434, 1433723730, 1433724035,
1433724370, 1433724634, 1433724936, 1433725236), class = c("POSIXct",
"POSIXt"), tzone = ""), UTM_X = c(636979.2503, 636977.6583,
637402.4471, 637400.3063, 637402.3105, 637407.1977, 637406.3305,
637408.2991, 637407.1907, 637407.8414, 637406.7617, 637407.1614,
637409.8019, 637431.5235, 637465.9644, 637495.9583, 637525.2219,
637573.6033, 637645.3501, 637683.3844, 637691.6229, 637693.4815,
637693.4973, 637691.2483, 637691.9061, 637693.6377, 637692.1106,
637692.3169, 637690.9989, 637691.4503, 637693.6252, 637692.4915,
637694.9434, 637692.6685, 637692.8116, 637694.6787, 637694.4404,
637695.9109, 637696.8945, 637695.2403, 637695.4283, 637694.6085,
637693.4962, 637695.6229, 637734.7283, 637773.2897, 637774.9891,
637787.6573, 637792.285, 637807.0486, 637834.6231, 637497.3348,
637149.9982, 637145.0345, 637178.159, 637181.8251, 637181.1075,
637178.023, 637175.327, 637179.9138, 637180.2833, 637181.5512,
637185.8749, 637181.0011, 637177.401, 637177.4498, 637176.787,
637176.0093, 637175.5126, 637177.9578, 637178.5819, 637188.3911,
637188.7303, 637189.496, 637204.3885, 637195.2063, 637204.9823,
637201.5235, 637212.3355, 637274.4294, 637293.0009, 637296.3954,
637331.3382, 637358.4369, 637365.1677, 637357.5562, 637355.3896,
637345.4827, 637339.1054, 628920.3789, 628869.9781, 630028.6781,
630156.4557, 629878.756, 629658.9786, 629412.6432, 629257.5965,
629405.8967, 629113.4479, 628955.5124, 628852.0231, 628711.9202,
628632.7134, 628621.7724, 628622.2565, 628683.6018, 628771.1182,
628790.8437, 628867.7592, 628881.9794, 628830.9898, 628681.9202,
628575.3395, 628578.1836, 628656.4902, 628659.2271, 628656.689,
628660.4677, 628657.294, 628657.077, 628689.6585, 628727.0131,
628716.6979, 628703.8397, 628678.6953, 628679.3594, 628681.3549,
628625.6275, 628563.1372, 628488.425, 628482.5023, 628469.2209,
628417.9697, 628407.7352, 628405.374, 628393.143, 628394.0092,
628396.2344, 628395.05, 628395.7787, 627684.7989, 627704.889,
627702.5528, 627702.0422, 627708.7906, 627706.9374, 627687.0371,
627622.0573, 627605.7932, 627603.5707, 627587.8803, 627606.0471,
627603.2967, 627602.954, 627603.5844, 627604.1232, 627601.697,
627581.6104, 627599.7062, 627616.327, 627661.7402, 627889.446,
627883.5896, 627803.1167, 627792.5918, 627716.0886, 627720.8854,
627671.8217, 627666.9994, 627586.7035, 627584.4273, 627532.492,
627502.6326, 627430.6781, 627408.8845, 627357.5049, 627406.0466,
627427.1382, 636666.3215, 636629.7032, 637179.9041, 637187.7067,
637183.5281, 637193.2082, 637227.2331, 637290.2543, 637347.9311,
637373.0887, 637368.8923, 637371.0722, 637383.95, 637480.1799,
637510.543, 637558.428, 637676.2714, 637682.3564, 637680.8591,
637682.8516, 637680.8317, 637680.8341, 637681.9818, 637681.2897,
637681.3658, 637681.9234, 637681.8824, 637682.0629, 637684.8756,
637681.602, 637682.7548, 637680.8578, 637682.9887, 637680.2496,
637681.4629, 637682.3731, 637682.2223, 637684.1076, 637681.7127,
637681.1249, 637681.6758, 637681.595, 637682.5253, 637702.3094,
637728.9487, 637784.0853, 637776.5727, 637785.2538, 637786.6413,
637807.9935, 637834.8672, 637485.5191, 637148.5674, 637139.2974,
637174.9104, 637191.9371, 637179.4262, 637175.7715, 637176.3455,
637174.5459, 637174.2012, 637173.7462, 637177.3967, 637176.6907,
637177.8458, 637178.0774, 637178.4151, 637178.3272, 637178.2442,
637177.6655, 637176.734, 637186.2713, 637185.0998, 637197.4201,
637197.9147, 637204.1485, 637203.1784, 637204.4993, 637205.3515,
637279.9058, 637303.773, 637303.5724, 637330.3473, 637354.416,
637366.5627, 637340.7274, 637357.5505, 637350.709, 637349.689
), UTM_Y = c(3365828.581, 3365826.066, 3364992.673, 3364991.006,
3364989.036, 3364990.816, 3364989.486, 3364991.849, 3364991.37,
3364990.059, 3364989.58, 3364991.403, 3364991.536, 3364985.614,
3365030.733, 3365054.446, 3365091.064, 3365138.444, 3365289.033,
3365390.111, 3365398.839, 3365387.124, 3365390.427, 3365386.696,
3365387.104, 3365379.344, 3365386.131, 3365388.805, 3365385.152,
3365385.158, 3365386.394, 3365385.637, 3365385.48, 3365386.071,
3365385.397, 3365387.416, 3365387.269, 3365389.505, 3365389.971,
3365387.833, 3365389.676, 3365390.685, 3365385.96, 3365384.934,
3365352.152, 3365369.878, 3365376.795, 3365390.013, 3365382.689,
3365382.189, 3365410.939, 3365683.847, 3365620.829, 3365574.121,
3365527.084, 3365501.513, 3365502.801, 3365512.739, 3365514.733,
3365512.885, 3365511.016, 3365512.562, 3365510.255, 3365511.235,
3365509.494, 3365509.439, 3365509.431, 3365509.388, 3365510.678,
3365509.534, 3365511.083, 3365511.85, 3365514.659, 3365513.371,
3365525.476, 3365526.036, 3365529.429, 3365528.676, 3365513.172,
3365507.793, 3365514.623, 3365512.105, 3365504.477, 3365512.401,
3365495.238, 3365490.863, 3365441.075, 3365411.542, 3365403.003,
3371496.516, 3371594.382, 3370587.966, 3370380.241, 3370270.012,
3370346.817, 3370433.295, 3370488.189, 3370225.222, 3370122.896,
3370174.202, 3370232.298, 3370192.371, 3370255.722, 3370283.548,
3370283.21, 3370305.674, 3370344.002, 3370354.4, 3370348.973,
3370200.353, 3370078.071, 3370123.589, 3370194.686, 3370393.878,
3370500.265, 3370498.635, 3370498.882, 3370497.663, 3370499.687,
3370500.172, 3370633.763, 3370704.904, 3370839.426, 3370879.943,
3370950.842, 3370957.988, 3370963, 3371031.496, 3371082.487,
3371109.89, 3371112.17, 3371118.807, 3371167.072, 3371168.581,
3371170.127, 3371178.074, 3371177.097, 3371178.11, 3371176.777,
3371178.482, 3371566.662, 3371622.632, 3371621.252, 3371619.772,
3371623.975, 3371627.245, 3371636.71, 3371612.734, 3371598.776,
3371590.192, 3371636.009, 3371656.352, 3371656.719, 3371656.471,
3371656.755, 3371659.1, 3371656.401, 3371688.243, 3371717.065,
3371741.492, 3371755.505, 3371618.156, 3371595.308, 3371615.82,
3371560.55, 3371552.166, 3371572.884, 3371547.544, 3371530.616,
3371559.755, 3371591.63, 3371612.877, 3371657.663, 3371727.149,
3371739.263, 3371823.645, 3371912.149, 3371969.549, 3366104.602,
3365712.344, 3365494.627, 3365496.045, 3365484.575, 3365475.02,
3365485.304, 3365467.377, 3365477.805, 3365507.809, 3365510.682,
3365519.888, 3365527.19, 3365491.394, 3365490.37, 3365468.274,
3365393.413, 3365389.355, 3365386.964, 3365391.977, 3365389.125,
3365388.937, 3365389.35, 3365389.375, 3365387.159, 3365387.133,
3365386.578, 3365386.735, 3365386.161, 3365387.472, 3365387.487,
3365387.064, 3365387.977, 3365385.016, 3365387.836, 3365388.036,
3365387.048, 3365389.909, 3365387.074, 3365384.939, 3365387.717,
3365388.026, 3365388.16, 3365385.728, 3365344.996, 3365374.693,
3365377.679, 3365387.866, 3365389.823, 3365391.779, 3365410.631,
3365698.174, 3365622.297, 3365571.954, 3365511.957, 3365510.265,
3365505.086, 3365509.196, 3365512.44, 3365513.438, 3365508.777,
3365509.049, 3365509.838, 3365506.403, 3365507.748, 3365510.711,
3365509.075, 3365507.666, 3365508.152, 3365505.285, 3365498.401,
3365508.531, 3365508.483, 3365513.538, 3365520.783, 3365519.376,
3365523.92, 3365529.634, 3365529.866, 3365498.661, 3365512.941,
3365509.801, 3365503.056, 3365513.548, 3365502.683, 3365482.215,
3365438.852, 3365412.317, 3365406.363)), .Names = c("CollarID",
"DateTime", "UTM_X", "UTM_Y"), row.names = c(NA, -267L), class = "data.frame")
Commenting out incorrect transformation
# chupacabra$DateTime <-as.POSIXct(strptime(chupacabra$DateTime, format='%m/%d/%Y %H:%M:%S'),origin='1970-01-01')
chupacabra2<-as.ltraj(chupacabra[, c("UTM_X","UTM_Y")], date=chupacabra$DateTime,id=chupacabra$CollarID, typeII=TRUE)
monster1<-chupacabra2[1] #extract the first chupacabra
monster2<-chupacabra2[2] #extract the second chupacabra
proxdf <-Prox(monster1,monster2, tc=0.5*60,dc=210, local =TRUE)
Here's a sample of chupcabras that we've been tracking. We'd like to examine how often they interact with each other. This dataset has 3 individuals (but we have many many more chupcabras) and it is inefficient to pull out the animals/creatures 1 by 1 to calculate proximity. I'd like to do a for loop (for i in unique ID, perhaps) but I don't understand how to do this when the data is in ltraj format. Any assistance would be appreciated.
After removing the harmful code that incorrectly reformatted the DateTime value, I plotted the "trajectory" for the 3 2-animal combinations. Clearly animal1 did not interact with animal 2 since their ranges were disjoint. Animal 1 and animal 3 do appear to interact, since about halfway through their joint sojourns they have the roughly same trajectory:
plot( chupacabra2[[1]]$x, chupacabra2[[1]]$y, type="l",
xlim=range( c(chupacabra2[[1]]$x, chupacabra2[[3]]$x)),
ylim= range(c(chupacabra2[[1]]$y, chupacabra2[[3]]$y)))
lines( chupacabra2[[3]]$x, chupacabra2[[3]]$y, col="red")
This appears in proxdf13 as:
> monster1<-chupacabra2[1]
>
> monster3<-chupacabra2[3]
>
> proxdf13 <-Prox(monster1,monster3, tc=0.5*60,dc=210, local =TRUE)
> proxdf13
date prox
1 2015-06-07 06:01:00 549.074863
2 2015-06-07 07:20:00 104.169909
3 2015-06-07 08:10:00 6.205875
4 2015-06-07 09:05:00 14.409016
5 2015-06-07 09:20:00 11.189309
6 2015-06-07 15:40:00 6.481131
7 2015-06-07 16:25:00 4.259042
8 2015-06-07 17:15:00 10.648210
9 2015-06-07 17:35:00 4.181297
10 2015-06-07 17:41:00 7.574566
So a guess that "interact" (might) means something along the lines of "is within 15 distance units for more than 2 days in succession". So a natural function to consider would be rle:
> rle( proxdf13$prox < 15 )
Run Length Encoding
lengths: int [1:2] 2 8
values : logi [1:2] FALSE TRUE
> RL13 <- rle( proxdf13$prox < 15 )
> max( RL13$lengths [ RL13$values] )
[1] 8
And test whether this is greater than some value, say 2?
So that was the method to handle a single 2way animal-animal combination. The comments of the questioner below suggest I may have lost him in the material that follows. To get the 2way combinations of a sequence use combn:
> combn(1:3, 2)
[,1] [,2] [,3]
[1,] 1 1 2
[2,] 2 3 3
That is a matrix that will be use to generate the index values for pulling two single animal "trajectory" dataframes at a time from the 'ltraj' object using each column separately. When the apply function is used it can "loop" over etiehr rows or columns of a matrix and using 2 as the index value to apply specifieds the columns.
So putting this all together (using apply to loop over the column indices of combn-result to get the two-way combinations):
( max.days.prox <- apply( combn( seq(length(chupacabra2)), 2 ), 2,
# loops over columns of the "combinations matrix"
function(x) {
proxcomb <- Prox( chupacabra2[ x[1] ], chupacabra2[ x[2] ],
tc=0.5*60,dc=210, local =TRUE)
RLcomb <- rle( proxcomb$prox < 15 )
Interact.days <- max( RLcomb$lengths [ RLcomb$values] ) } ) )
# [1] -Inf 8 -Inf
We rbind that to the result to look at the items of interest:
> rbind(combn( seq(length(chupacabra2)), 2 ) , max.days.prox)
[,1] [,2] [,3]
1 1 2
2 3 3
max.days.prox -Inf 8 -Inf
So only the pairing of animal1 and animal3 provided evidence of an interaction. This would generalize to larger instances of ltraj-objects

How to determine similar values around a particular row in R?

Goal
I want to find the duration of lane changes of vehicles as shown in the plot, using lateral position data.
Data
Following is the data for only one vehicle:
> dput(a)
structure(list(Frame.ID = 526:1058, xcoord = c(14.346, 14.367,
14.388, 14.419, 14.458, 14.503, 14.55, 14.6, 14.65, 14.702, 14.754,
14.807, 14.86, 14.913, 14.966, 15.02, 15.072, 15.125, 15.178,
15.23, 15.282, 15.333, 15.384, 15.434, 15.482, 15.529, 15.574,
15.617, 15.657, 15.694, 15.727, 15.755, 15.78, 15.802, 15.823,
15.841, 15.858, 15.874, 15.889, 15.903, 15.917, 15.93, 15.942,
15.955, 15.967, 15.978, 15.989, 16, 16.011, 16.022, 16.033, 16.044,
16.055, 16.065, 16.075, 16.085, 16.095, 16.104, 16.112, 16.12,
16.129, 16.139, 16.151, 16.164, 16.178, 16.195, 16.212, 16.231,
16.25, 16.27, 16.291, 16.312, 16.333, 16.356, 16.379, 16.403,
16.428, 16.455, 16.482, 16.511, 16.542, 16.574, 16.609, 16.646,
16.687, 16.732, 16.783, 16.839, 16.902, 16.967, 17.033, 17.1,
17.168, 17.232, 17.294, 17.354, 17.41, 17.464, 17.513, 17.559,
17.6, 17.636, 17.665, 17.685, 17.694, 17.7, 17.708, 17.725, 17.751,
17.782, 17.817, 17.856, 17.897, 17.939, 17.982, 18.025, 18.067,
18.108, 18.145, 18.178, 18.207, 18.232, 18.255, 18.274, 18.292,
18.308, 18.323, 18.336, 18.349, 18.361, 18.372, 18.383, 18.393,
18.403, 18.413, 18.422, 18.432, 18.441, 18.451, 18.46, 18.469,
18.479, 18.488, 18.496, 18.505, 18.513, 18.521, 18.529, 18.537,
18.544, 18.55, 18.556, 18.562, 18.567, 18.574, 18.58, 18.588,
18.597, 18.609, 18.623, 18.64, 18.662, 18.69, 18.722, 18.76,
18.802, 18.849, 18.899, 18.953, 19.012, 19.076, 19.144, 19.218,
19.299, 19.386, 19.479, 19.574, 19.669, 19.763, 19.855, 19.945,
20.031, 20.112, 20.187, 20.254, 20.31, 20.352, 20.385, 20.412,
20.435, 20.45, 20.455, 20.449, 20.436, 20.416, 20.39, 20.361,
20.328, 20.293, 20.256, 20.217, 20.178, 20.139, 20.1, 20.063,
20.026, 19.99, 19.957, 19.925, 19.895, 19.867, 19.842, 19.819,
19.796, 19.774, 19.751, 19.729, 19.707, 19.685, 19.662, 19.64,
19.617, 19.594, 19.571, 19.547, 19.523, 19.499, 19.473, 19.449,
19.426, 19.404, 19.382, 19.359, 19.336, 19.312, 19.288, 19.263,
19.237, 19.211, 19.184, 19.156, 19.127, 19.097, 19.066, 19.033,
18.998, 18.961, 18.921, 18.878, 18.831, 18.781, 18.727, 18.67,
18.612, 18.554, 18.498, 18.446, 18.397, 18.349, 18.304, 18.264,
18.233, 18.21, 18.194, 18.182, 18.175, 18.171, 18.17, 18.172,
18.177, 18.183, 18.192, 18.202, 18.213, 18.226, 18.241, 18.258,
18.277, 18.298, 18.321, 18.346, 18.371, 18.396, 18.422, 18.447,
18.471, 18.495, 18.518, 18.54, 18.559, 18.577, 18.591, 18.601,
18.606, 18.605, 18.6, 18.593, 18.584, 18.579, 18.58, 18.59, 18.607,
18.629, 18.655, 18.682, 18.711, 18.739, 18.766, 18.792, 18.818,
18.842, 18.864, 18.885, 18.905, 18.924, 18.943, 18.961, 18.98,
19, 19.02, 19.038, 19.054, 19.068, 19.081, 19.092, 19.103, 19.112,
19.121, 19.129, 19.137, 19.144, 19.15, 19.156, 19.161, 19.166,
19.169, 19.172, 19.173, 19.173, 19.171, 19.168, 19.163, 19.156,
19.147, 19.136, 19.123, 19.109, 19.093, 19.078, 19.061, 19.041,
19.017, 18.988, 18.954, 18.918, 18.878, 18.836, 18.795, 18.756,
18.722, 18.693, 18.671, 18.655, 18.642, 18.633, 18.625, 18.619,
18.613, 18.608, 18.602, 18.593, 18.58, 18.562, 18.537, 18.504,
18.46, 18.403, 18.33, 18.234, 18.115, 17.972, 17.806, 17.623,
17.427, 17.223, 17.013, 16.802, 16.592, 16.389, 16.191, 15.998,
15.806, 15.604, 15.386, 15.149, 14.891, 14.617, 14.328, 14.029,
13.722, 13.412, 13.097, 12.773, 12.436, 12.084, 11.723, 11.361,
11.006, 10.663, 10.334, 10.02, 9.723, 9.453, 9.219, 9.027, 8.874,
8.753, 8.657, 8.583, 8.525, 8.481, 8.448, 8.421, 8.4, 8.384,
8.371, 8.36, 8.351, 8.345, 8.338, 8.33, 8.319, 8.304, 8.284,
8.258, 8.224, 8.183, 8.136, 8.084, 8.029, 7.971, 7.912, 7.853,
7.794, 7.736, 7.681, 7.629, 7.581, 7.54, 7.506, 7.482, 7.468,
7.46, 7.459, 7.462, 7.468, 7.477, 7.489, 7.501, 7.514, 7.526,
7.539, 7.55, 7.562, 7.573, 7.584, 7.595, 7.607, 7.62, 7.636,
7.654, 7.675, 7.702, 7.734, 7.773, 7.823, 7.885, 7.96, 8.046,
8.134, 8.213, 8.278, 8.322, 8.342, 8.338, 8.308, 8.258, 8.192,
8.112, 8.023, 7.927, 7.827, 7.725, 7.623, 7.522, 7.424, 7.334,
7.252, 7.183, 7.128, 7.093, 7.078, 7.085, 7.117, 7.177, 7.267,
7.385, 7.525, 7.679, 7.839, 8, 8.155, 8.296, 8.418, 8.519, 8.606,
8.682, 8.749, 8.82, 8.891, 8.956, 9.012, 9.057, 9.09, 9.126,
9.162, 9.197, 9.227, 9.249, 9.257, 9.254, 9.251, 9.247), Lane = c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L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tbl", "data.frame"))
Method
To estimate the lane change duration, I want to start from the row where the target lane number is reported first time. In the example the target lane is 1 and the number 1 is reported first time when the vehicle touches the pavement marking, as shown in the plot. This point can be added in the data as:
a$rows <- as.numeric(row.names(a)) # Row numbers
a$lch <- a$xcoord[match(head(which(a$Lane==1),1), a$rows)]
For this point I want to find the difference from every other point.
a$difference <- abs(a$lch - a$xcoord)
Then, comparing the differences in both directions, there will be some rows where the differences will be quite similar indicating that lateral position was almost similar. The first instances in both directions will be treated as the limits of lane change maneuver.
How can I achieve this comparison part in R? I don't know how to 'move' from Frame.ID 929 (this example) upwards and downwards to compare where the differences become similar.
Not sure if it's the best/fastest solution, but the idea is to calculate the differences between two consecutive "difference" columns you created and pick a small value as a threshold to flag that they started becoming similar. Then get the row closest to the time point of the change for each lane.
Note that I renamed your dataset as dt. Run the process step by step to see how the process works. If you are happy with it you can make the script shorter by combining some commands.
dt$rows <- as.numeric(row.names(dt)) # Row numbers
dt$lch <- dt$xcoord[match(head(which(dt$Lane==1),1), dt$rows)]
dt$difference <- abs(dt$lch - dt$xcoord)
library(dplyr)
dt %>%
mutate(frameIDchange = Frame.ID[difference==0], # spot the frame ID of the change
diff_diff = difference - lag(difference, default=difference[1]), # find the difference of two consequtive differences
flag = ifelse(abs(diff_diff) <= 0.01,1,0)) %>% # flag if the difference of two consequtive differences is equal or less to our threshold (0.01)
filter(flag==1) %>% # keep only rows with threshold difference
mutate(frameIDdiff = abs(Frame.ID-frameIDchange)) %>% # calculate distance of frame IDs
group_by(Lane) %>% # for each lane
filter(frameIDdiff== min(abs(frameIDdiff))) # return the frame id closest to the change frame id with a difference equal or less to our threshold
# Frame.ID xcoord Lane rows lch difference frameIDchange diff_diff flag frameIDdiff
# 1 896 18.593 2 371 12.436 6.157 929 -0.009 1 33
# 2 953 8.351 1 428 12.436 4.085 929 0.009 1 24
This tells you that the frame ids where the differences started to become similar (0.01 threshold) are 896 and 953 for lanes 2 and 1 respectively. Obviously the frame id of the change (929) belongs between those frames as expected.
You can also experiment with slightly higher/less threshold values to see how the results change.

Resources