2016-11-09 41 views
1

我是新来的R.我试图用pca和ggbiplot来显示pca结果,但以某种方式卡住了一些我无法解决的错误。也许我的数据有问题,因为代码可以与其他数据一起使用。 我把代码和数据文件,我的情况下,使用您想在下面的链接继续重建场景: -R pca ggbiplot错误:更换有36行,数据有35

https://drive.google.com/drive/folders/0B2jQ7Vh3S3PaZkt3Y2ZyaV9XaXc

代码:PCA-plot.R 数据文件1:DAT1。 rda(这个工作正常) 数据文件2:dat2.rda(这个有问题)

感谢任何帮助。 我得到的错误是在底部。

谢谢 - 我们

> g <- ggbiplot(tr.pca, obs.scale = 1, var.scale = 1, 
+    groups = Ydfall, 
+    ellipse = TRUE, 
+    circle = TRUE) 
Error in `$<-.data.frame`(`*tmp*`, "groups", value = c(1L, 1L, 1L, 1L, : 
    replacement has 36 rows, data has 35 
> g <- g + scale_color_discrete(name = '') 
Scale for 'colour' is already present. Adding another scale for 'colour',  which will replace the existing scale. 
> g <- g + theme(legend.direction = 'horizontal', 
+    legend.position = 'top') 
> g<- g+ geom_point(size=1, shape=1, color="black", stroke=2) 
> 
> print(g) 
> 

回答

0

dfalldat2.rdaNA(试行which(is.na(dfall), arr.ind = T)),它会导致你的问题。您在使用prcomp()时使用了na.omit(),但在制作Ydfall时未使用。

Ydfall <- na.omit(dfall)[,1] # quick fix 

# but if I were you, I would do first; 
dfall <- na.omit(dfall)