/
cluster.R
54 lines (40 loc) · 1000 Bytes
/
cluster.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
library(data.table)
##graphics.off()
library(Rcpp)
#library(fpc)
sourceCpp("path.cpp")
if(!exists("pl")) {
pl = readRDS("gpsl_new.rds")
}
kv = c(308, 78, 165, 452, 21, 106, 8, 30, 639, 456, 53, 484, 453)
kv = 10*kv
##kv = rep(10,13)
make_paths = function(p, k) {
p <- p[,1:2]
#cl <- kmeans(p, centers=k, algorithm="MacQueen", iter=50)
cl <- kmeans(p, centers=k, nstart=2)
centers <- cl[["centers"]]
x <- as.vector(centers[,1])
y <- as.vector(centers[,2])
path <- c(x,y)
idx <- find_path(path) + 1
dt <- data.table(gps_long= x[idx], gps_lat = y[idx])
}
make_paths2 = function(p, k) {
print(k)
p <- p[,1:2]
cl <- kmeans(p, centers=k)
#cl <- kmeans(p, centers=k)
centers <- cl[["centers"]]
x <- as.vector(centers[,1])
y <- as.vector(centers[,2])
path <- c(x,y)
return(path)
}
paths = lapply(p, make_paths)
myplot = function(p) {
plot(p)
lines(p)
}
par(mfrow=c(4,4))
lapply(paths, myplot)