2016-11-09 46 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)