【发布时间】:2017-10-19 19:40:10
【问题描述】:
我以为是这个
kmeans(x = matrix(1:50, 5), centers = 2, iter.max = 10)
可以写成:
matrix(1:50, 5) %>%
map( ~kmeans(x = .x, centers = 2, iter.max = 10))
Error in sample.int(m, k) :
cannot take a sample larger than the population when 'replace = FALSE'
但是第二个不起作用。如何将kmeans 与purrr::map() 结合使用?
【问题讨论】:
-
这里为什么需要
map?matrix(1:50, 5) %>% kmeans(., centers = 2, iter.max = 10)。matrix是具有暗淡属性的vector。当您执行map时,它会遍历每个单独的观察结果。 -
@akrun 因为在我原来的例子中我有几个矩阵(缩放,有/没有某些变量等),我想比较聚类的结果。
-
不确定我明白了。如果
list中有多个矩阵,则可以应用map -
如果您的方法在
list即list(matrix(1:50, 5), matrix(51:100, 5)) %>% map( ~kmeans(x = .x, centers = 2, iter.max = 10))中,您的方法将有效