2013-06-23 45 views
1

我有已被分成兩個簇(如在this question討論的)的載體:根據R中羣集第一向量聚類第二向量

x <- c(1, 4, 5, 6, 9, 29, 32, 46, 55) 
    tree <- hclust(dist(x), method = "single") 
    split(x, cutree(tree, h = 19)) 
    # $`1` 
    # [1] 1 4 5 6 9 
    # 
    # $`2` 
    # [1] 29 32 46 55 

現在假設我具有相同的長度的另一集羣,這是我希望分成由同指數爲x的相同數目的簇,採取以下向量y作爲示例:

set.seed(77) 
    y = rnorm(9) 
    y 
    #[1] -0.54964 1.09105 0.63978 1.04258 0.16970 1.13780 -0.97055 -0.13183 
    #[9] 0.14623 

期望的結果應該是這樣的:

# $`1` 
    # [1] -0.54964 1.09105 0.63978 1.04258 0.16970 
    # 
    # $`2` 
    # [1] 1.13780 -0.97055 -0.13183 0.14623 

回答

1

就像你一樣的x

split(y, cutree(tree, h = 19)) 

而且因爲你現在在多個地方使用cutree(tree, h = 19),你還不如把它分配給一個變量:

groups <- cutree(tree, h = 19) 
split(x, groups) 
split(y, groups) 
相關問題