有没有办法按R中的近似值进行分组?

时间:2017-05-30 12:37:25

标签: r dplyr

我想在R,a"模糊分组"中近似地对值进行分组。如果你愿意的话。假设您有以下值:

c( 1.0, 105.4, 1.5, 48.7, 1.2, 0.9, 50.0, 51.0, 100.0)

我正在寻找的分组将是:

c( 1.0, 1.5, 1.0, 0.9)
c( 48.7, 50.0, 51.0)
c( 105.4 )

是否存在可以执行此操作的功能?如果没有,是否有一种有效的方法可以解决?

1 个答案:

答案 0 :(得分:3)

您想要进行1d群集。例如:

x <- c( 1.0, 105.4, 1.5, 48.7, 1.2, 0.9, 50.0, 51.0, 100.0)
kmeans(x, 3) #three groups
K-means clustering with 3 clusters of sizes 3, 4, 2

Cluster means:
    [,1]
1  49.90
2   1.15
3 102.70

Clustering vector:
[1] 2 3 2 1 2 2 1 1 3

Within cluster sum of squares by cluster:
[1]  2.66  0.21 14.58
 (between_SS / total_SS =  99.9 %)

Available components:

[1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss" "betweenss"    "size"         "iter"         "ifault"
split(x, kmeans(x, 3)$cluster)
$`1`
[1] 48.7 50.0 51.0

$`2`
[1] 105.4 100.0

$`3`
[1] 1.0 1.5 1.2 0.9