# create data for two datasets
data <- data.frame(x = c(1.5, 2, 4, 5, 4, 6, 5.5, 8),
y = c(6, 4, 5.5, 3, 3.5, 5.5, 7, 6),
dataset = rep(c(1, 2), each = 4))
plot(data$x, data$y, pch = c(21, 19)[data$dataset])
# divide into the two datasets and calculate Hamilton path
X1 <- data[1:4, ]
X2 <- data[5:8, ]
if(requireNamespace("rlemon", quietly = TRUE)) {
E <- HamiltonPath(X1, X2)
# plot the resulting edges
segments(x0 = data$x[E[, 1]], y0 = data$y[E[, 1]],
x1 = data$x[E[, 2]], y1 = data$y[E[, 2]],
lwd = 2)
}
Run the code above in your browser using DataLab