我正在使用
dplyr
. 当使用
group_by
函数中,有些变量的方差相等,因此主成分分析无法运行。如何删除任何方差相等的列,然后执行
prcomp
剩下什么?下面是虚拟数据。谢谢您。
为刚毛萼片长度加上相等的方差。
library(dplyr)
iris[1:50,1]<-0
尝试以等方差运行PCA
> iris%>%
+ group_by(Species)%>%
+ group_map(~prcomp(.[,1:4], scale.=T))
Error in prcomp.default(.[, 1:4], scale. = T) :
cannot rescale a constant/zero column to unit variance
检查等方差
> iris%>%
+ group_by(Species)%>%
+ group_map(~names(.[,1:4][, sapply(.[,1:4], function(v) var(v, na.rm=TRUE)==0)]))
[[1]]
[1] "Sepal.Length"
[[2]]
character(0)
[[3]]
character(0)
尝试排除等方差列并运行pcas
> iris%>%
+ group_by(Species)%>%
+ group_map(~sapply(.[,1:4], function(v) var(v, na.rm=TRUE)>0))%>%
+ group_map(~prcomp(.[,1:4], scale.=T))
Error in UseMethod("group_split") :
no applicable method for 'group_split' applied to an object of class "list"