与标题中一样,有没有一种方法可以使用purrr
和dplyr
从列表中提取多个组件? pluck
适用于单个元素,但不适用于少数元素。如下所示:
my_data <- data.frame(group = c(sample(c('A', 'B', 'C'), 20, replace = TRUE)), x = runif(100, 0, 10), y = runif(100, 0, 10))
my_data %>%
group_by(group) %>%
nest() %>%
mutate(km_cluster = map(data, ~kmeans(.x, 3) %>% pluck(c('cluster', 'centers'))))
然后,我想添加有关将观察值分配给data
的簇号和簇质心的信息。数据列中单个元素的期望输出如下所示:
structure(list(x = c(7.73117371369153, 0.0510848499834538, 4.55259998561814,
9.89025634946302, 2.37372878007591, 1.97317335521802), y =
c(7.59347913088277, 8.7801841692999, 9.11954281385988, 3.90361216617748,
2.92225106153637, 0.338000932242721), cluster = c(3L, 1L, 1L, 3L, 2L, 2L),
x_center = c(7.99236144404858, 2.53133282822091, 2.53133282822091,
7.99236144404858, 3.79731344497379, 3.79731344497379), y_center =
c(6.60092391962694, 8.42530809265251,8.42530809265251, 6.60092391962694,
2.02696633155403, 2.02696633155403)), .Names = c("x", "y", "cluster",
"x_center", "y_center"), row.names = c(NA,
-6L), class = c("tbl_df", "tbl", "data.frame"))
答案 0 :(得分:3)
我们可以在底数R中使用.collapse {
flex: 0 0 50%;
max-width: 66.6666%;
padding: 30px;
background-color: red;
transition: flex 1000ms ease;
}
.expand {
flex: 0 0 66.66667%;
background-color: red;
}
。
[
更新
my_data %>%
group_by(group) %>%
nest() %>%
mutate(km_cluster = map(data, ~kmeans(.x, 3) %>% `[`(c("cluster", "centers"))))
# # A tibble: 3 x 3
# group data km_cluster
# <fct> <list> <list>
# 1 B <tibble [25 x 2]> <list [2]>
# 2 C <tibble [60 x 2]> <list [2]>
# 3 A <tibble [15 x 2]> <list [2]>