我有一个主表,其中包含每个personid的主要事件的日期:
dfMain <- data.frame(last = c("2017-08-01", "2017-08-01", "2017-08-05","2017-09-02","2017-09-02"),
previous = c(NA, NA, "2017-08-01", "2017-08-05", "2017-08-01"),
personid = c(12341, 122345, 12341, 12341, 122345),
diff = c(NA, NA, 4, 28, 32))
Run Code Online (Sandbox Code Playgroud)
("之前"和"差异"变量上的NA表示此人员有他的第一个"主要偶数"即:没有以前的日期,没有时差)
我还有一个辅助表,其中包含每个personid的"辅助事件":
dfSecondary <- data.frame(date = c("2017-09-01", "2017-08-30", "2017-08-04", "2017-08-02", "2017-08-02"),
personid = c(122345, 122345, 12341, 122345, 12341))
Run Code Online (Sandbox Code Playgroud)
我的问题是,什么是最佳方式(由于我的数据量)增加我的"dfMain"数据框与每个personid的主要事件日期之间的唯一次要事件的数量.
在虚拟示例中,我的目标是获取此表:
Occurances <- c(NA, NA, 2, 0, 3)
dfObjective <- data.frame(dfMain, Occurances)
Run Code Online (Sandbox Code Playgroud) 我的目标是比较我使用过的两种聚类方法中的哪一种并且聚类平方cluster_method_1和cluster_method_2最大,以确定哪一种实现更好的分离。
我基本上是在寻找一种有效的方法来计算集群 1 的每个点与集群 2、3、4 的所有点之间的距离,依此类推。
示例数据框:
structure(list(x1 = c(0.01762376, -1.147739752, 1.073605848,
2.000420899, 0.01762376, 0.944438811, 2.000420899, 0.01762376,
-1.147739752, -1.147739752), x2 = c(0.536193126, 0.885609849,
-0.944699546, -2.242627057, -1.809984553, 1.834120637, 0.885609849,
0.96883563, 0.186776403, -0.678508604), x3 = c(0.64707104, -0.603759684,
-0.603759684, -0.603759684, -0.603759684, 0.64707104, -0.603759684,
-0.603759684, -0.603759684, 1.617857394), x4 = c(-0.72712328,
0.72730861, 0.72730861, -0.72712328, -0.72712328, 0.72730861,
0.72730861, -0.72712328, -0.72712328, -0.72712328), cluster_method_1 = structure(c(1L,
3L, 3L, 3L, 2L, 2L, 3L, 2L, 1L, 4L), .Label = c("1", "2", "4",
"6"), class = "factor"), …Run Code Online (Sandbox Code Playgroud)