【发布时间】:2019-09-24 06:17:44
【问题描述】:
我正在使用dplyr 管道对数据集中的组运行 pcas。我从group_split 开始,所以我正在处理一个列表。为了运行prcomp() 函数,只能包含每个列表的numeric 列,但我希望将factor 列带回最后进行绘图。我尝试在管道的中途使用{. ->> temp} 保存中间输出,但由于它是一个列表,我不知道如何在绘图时索引分组列。
library(tidyverse)
library(ggbiplot)
iris %>%
group_split(Species, keep = T) %>% #group by species, one pca per species
{. ->> temp} %>% # save intermediate output to preserve species column for use in plotting later
map(~.x %>% select_if(is.numeric) %>% select_if(~var(.) != 0) %>%
prcomp(scale. = TRUE))%>% #run pca on numeric columns only
map(~ggbiplot(.x), label=temp$Species)#plot each pca, labeling points as species names form the temporary object
这可以为iris数据集中的每个物种生成一个pca图,但由于temp$species = NULL,这些点没有被标记。
【问题讨论】:
-
能否先保存
temp <- unique(iris$Species)而不将其保存为中间输出,然后在map(~ggbiplot(.x), label=temp)中使用?ggbiplot也不适用于 R 3.6.1 吗?